By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,465 Members | 1,448 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,465 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
> 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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.