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

L

laclac01

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?
 
M

marcas

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
 
V

Victor Bazarov

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
 
I

Ian

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
 
C

Chris Hulbert

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 :)!
 
S

Stephen Howe

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 it is expecting a numeric textual floating point number with optional
whitespace beforehand e.g. " 1.6180339"
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
 
P

Phillip Rhodes

Victor Bazarov wrote:

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
 
R

red floyd

Phillip said:
Victor Bazarov wrote:




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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top