473,386 Members | 1,830 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,386 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 4192
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.