473,287 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

reading a binary file in C++ and having trouble.

So I am converting some matlab code to C++. I am stuck at one part of
the code. The matlab code uses fread() to read in to a vector a file.
It's a binary file. The vector is made up of floats, which in matlab
is 32 bits. How do I get this binary file in to floats in c++?
I try reading the file using the ifstream>>myFloat. But nothing ever
goes in to the float. So the closest I have come is having 4 unsigned
char store the binary data. But from there I don't know how to get
it in to a float? When I try to do:
myFloat<<myChar1<<8;
myFloat<<myChar2<<8;
myFloat<<myChar3<<8;
myFloat<<myChar4<<8;

I don't get the correct answer.
myFloat being the float and myChar1, myChar2, myChar3, myChar4 being
the 4 unsigned chars that have the binary data from the file in them.

Using a hex editor I have verified that the four unsigned chars do have
the correct value in them.

Does matlab represent floats different?

Jul 23 '05 #1
7 4183
la******@yahoo.com schrieb:
So I am converting some matlab code to C++. I am stuck at one part of
the code. The matlab code uses fread() to read in to a vector a file.
It's a binary file. The vector is made up of floats, which in matlab
is 32 bits. How do I get this binary file in to floats in c++?
I try reading the file using the ifstream>>myFloat. But nothing ever
goes in to the float. So the closest I have come is having 4 unsigned
char store the binary data. But from there I don't know how to get
it in to a float? When I try to do:
myFloat<<myChar1<<8;
myFloat<<myChar2<<8;
myFloat<<myChar3<<8;
myFloat<<myChar4<<8;

I don't get the correct answer.
myFloat being the float and myChar1, myChar2, myChar3, myChar4 being
the 4 unsigned chars that have the binary data from the file in them.

Using a hex editor I have verified that the four unsigned chars do have
the correct value in them.

Does matlab represent floats different?


I don`t know how float is implemented in matlab. In my implementation of
C (C++) it is implemented the following way (IEEE):

float: 1Bit Sign/ 8Bits Exponent / 23Bits Mantisse = 32 Bits

Maybe this split ist different in matlab (in ammount of bits per
component, and the order of the components) ... but i don´t think so

In pure C i would solve it the following way:

matlab float -> bytes-> C/C++ float
in C/C++ you can do then

union {
float mycfloat;
unsigned char frommatlab [sizeof(float)];
} conversion;

and write the bytes you got from matlab into conversion.frommatlab[...]
and read out the float with conversion.mycfloat

regards marcas

Jul 23 '05 #2
la******@yahoo.com wrote:
So I am converting some matlab code to C++. I am stuck at one part of
the code. The matlab code uses fread() to read in to a vector a file.
It's a binary file. The vector is made up of floats, which in matlab
is 32 bits. How do I get this binary file in to floats in c++?
I try reading the file using the ifstream>>myFloat.
Don't. operator >> is for _formatted_ input. If your files is binary,
you don't want formatted input, you want to use the 'read' member.
But nothing ever
goes in to the float. So the closest I have come is having 4 unsigned
char store the binary data. But from there I don't know how to get
it in to a float? When I try to do:
myFloat<<myChar1<<8;
myFloat<<myChar2<<8;
myFloat<<myChar3<<8;
myFloat<<myChar4<<8;

I don't get the correct answer.
Of course you don't. << operation is not for combining bytes into
a float. Use

yourfstream.read(&yourFloat, sizeof(float));
myFloat being the float and myChar1, myChar2, myChar3, myChar4 being
the 4 unsigned chars that have the binary data from the file in them.

Using a hex editor I have verified that the four unsigned chars do
have the correct value in them.

Does matlab represent floats different?


We don't know that. Ask in a Matlab newsgroup.

V
Jul 23 '05 #3
Ian
la******@yahoo.com wrote:
I don't get the correct answer.
myFloat being the float and myChar1, myChar2, myChar3, myChar4 being
the 4 unsigned chars that have the binary data from the file in them.

Using a hex editor I have verified that the four unsigned chars do have
the correct value in them.

Does matlab represent floats different?

Have you tried reversing the byte order?

Ian
Jul 23 '05 #4
la******@yahoo.com wrote:
So I am converting some matlab code to C++. I am stuck at one part of
the code. The matlab code uses fread() to read in to a vector a file.
It's a binary file. The vector is made up of floats, which in matlab
is 32 bits. How do I get this binary file in to floats in c++?
I try reading the file using the ifstream>>myFloat. But nothing ever
goes in to the float. So the closest I have come is having 4 unsigned
char store the binary data. But from there I don't know how to get
it in to a float? When I try to do:
myFloat<<myChar1<<8;
myFloat<<myChar2<<8;
myFloat<<myChar3<<8;
myFloat<<myChar4<<8;

I don't get the correct answer.
myFloat being the float and myChar1, myChar2, myChar3, myChar4 being
the 4 unsigned chars that have the binary data from the file in them.

Using a hex editor I have verified that the four unsigned chars do have
the correct value in them.

Does matlab represent floats different?


Matlab uses IEEE 754 floating point format. In C, you would do:

fp = fopen(file,"rb");
fread(file,sizeof(float),number_of_floats_to_read, fp);
fclose(fp);

C++, i think somethng like:

ifstream is;
is.open(file,infile::binary | infile::in);
is.read((char *)&f,sizeof(float)*number_of_floats);
is.close();
If my C++ is ugly, it's b/c I avoid it except for GUI (wxWidgets). My
C is good though :)!

Jul 23 '05 #5
> So I am converting some matlab code to C++. I am stuck at one part of
the code. The matlab code uses fread() to read in to a vector a file.
It's a binary file. The vector is made up of floats, which in matlab
is 32 bits. How do I get this binary file in to floats in c++?
I try reading the file using the ifstream>>myFloat. But nothing ever
goes in to the float.

operator is used to read from formatted text files _NOT_ binary files. So it is expecting a numeric textual floating point number with optional
whitespace beforehand e.g. " 1.6180339" operator in C++ is analogous to fscanf() in C when reading from a file.


The analogous equivalent to fread() in C for C++ is .read() member function
of ifstream. You use this to read binary files.
This is what you want.

Stephen Howe
Jul 23 '05 #6
Victor Bazarov wrote:

Does matlab represent floats different?

We don't know that. Ask in a Matlab newsgroup.


But wouldn't the Matlab group just send him back here,
claiming ignorance of C++? Seems like one could get
caught in a nasty loop that way... :-)
TTYL,

Phil
--
North Carolina - First In Freedom

Free America - Vote Libertarian
www.lp.org
Jul 23 '05 #7
Phillip Rhodes wrote:
Victor Bazarov wrote:

Does matlab represent floats different?


We don't know that. Ask in a Matlab newsgroup.

But wouldn't the Matlab group just send him back here,
claiming ignorance of C++? Seems like one could get
caught in a nasty loop that way... :-)


No, MATLAB specifically defines an interface to both C and C++,
therefore it's on-topic over there.
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chuck Anderson | last post by:
I've read some discussions, searched Google, tried pack, unpack, bin2hex, explode, implode, and I just can't figure out how to convert this data. I am trying to read a file with the following...
8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
2
by: nnimod | last post by:
Hi. I'm having trouble reading some unicode files. Basically, I have to parse certain files. Some of those files are being input in Japanese, Chinese etc. The easiest way, I figured, to distinguish...
2
by: Jack | last post by:
Hi I am having a little trouble trying to read a binary file, I would like to write an ascii to Metastock converter in python but am not having a lot of success. The file formats are ...
1
by: Mr X | last post by:
One of my employer's products, written in VB6, reads and writes binary files. We are shortly going to port to VB.NET (framework version 2.0) and will need to be able to read and write these binary...
31
by: tophandasa | last post by:
Hi all, I'm having a trouble reading a binary file as float values.I have to read the data in binary mode, then read every four bytes into a float variable. I have done my search, but i found out...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
8
by: Bryan.Fodness | last post by:
Hello, I am having trouble writing the code to read a binary string. I would like to extract the values for use in a calculation. Any help would be great. Here is my function that takes in...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.