468,512 Members | 1,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Binary Files

I'm very new to C++ coming in from using C# and the like. I've read a
lot of good posts about binary files but I'm still missing a few key
ingredients that will help me with the following problem.

I have a binary file that has a specific number of floats per row. I
need to read each float into a float array for processing by another
block of code (calculations). Each row gets read into this array and
processed before the next row gets read.

How do I read in from a binary file and store into a float array?
How do I keep track of the position I'm at in the file so I can process
each row individually?

Thanks in advance for the assistance.
Jason

Jul 19 '06 #1
8 3743
"Ronin" writes:
I'm very new to C++ coming in from using C# and the like. I've read a
lot of good posts about binary files but I'm still missing a few key
ingredients that will help me with the following problem.

I have a binary file that has a specific number of floats per row. I
need to read each float into a float array for processing by another
block of code (calculations). Each row gets read into this array and
processed before the next row gets read.

How do I read in from a binary file and store into a float array?
How do I keep track of the position I'm at in the file so I can process
each row individually?
The person who wrote the file identified he rows in some way - either by
knowing there were always exactly n floats per row, or by writing a header
or mark that could not be mistaken for a float. You have to find out what
rules were used when the file was written.
Jul 19 '06 #2
Ronin wrote:
I'm very new to C++ coming in from using C# and the like. I've read a
lot of good posts about binary files but I'm still missing a few key
ingredients that will help me with the following problem.

I have a binary file that has a specific number of floats per row. I
need to read each float into a float array for processing by another
block of code (calculations). Each row gets read into this array and
processed before the next row gets read.

How do I read in from a binary file and store into a float array?
How do I keep track of the position I'm at in the file so I can process
each row individually?
Use std::ifstream::read(), not the extraction operator (>>). You'll
need an ugly reinterpret_cast from float, and you'll want to make sure
the binary format for the floats in your file is the same as those for
your program. The binary float representation is not standardized in
C++, and different implementations can do it differently (this is a
good reason to prefer text instead of binary when possible).

Cheers! --M

Jul 19 '06 #3

osmium wrote:
"Ronin" writes:
I'm very new to C++ coming in from using C# and the like. I've read a
lot of good posts about binary files but I'm still missing a few key
ingredients that will help me with the following problem.

I have a binary file that has a specific number of floats per row. I
need to read each float into a float array for processing by another
block of code (calculations). Each row gets read into this array and
processed before the next row gets read.

How do I read in from a binary file and store into a float array?
How do I keep track of the position I'm at in the file so I can process
each row individually?

The person who wrote the file identified he rows in some way - either by
knowing there were always exactly n floats per row, or by writing a header
or mark that could not be mistaken for a float. You have to find out what
rules were used when the file was written.
I'll know that there are n floats per row with a variable number of
rows during any given run of the program.

Jul 19 '06 #4

mlimber wrote:
Ronin wrote:
I'm very new to C++ coming in from using C# and the like. I've read a
lot of good posts about binary files but I'm still missing a few key
ingredients that will help me with the following problem.

I have a binary file that has a specific number of floats per row. I
need to read each float into a float array for processing by another
block of code (calculations). Each row gets read into this array and
processed before the next row gets read.

How do I read in from a binary file and store into a float array?
How do I keep track of the position I'm at in the file so I can process
each row individually?

Use std::ifstream::read(), not the extraction operator (>>). You'll
need an ugly reinterpret_cast from float, and you'll want to make sure
the binary format for the floats in your file is the same as those for
your program. The binary float representation is not standardized in
C++, and different implementations can do it differently (this is a
good reason to prefer text instead of binary when possible).

Cheers! --M
How do you do a reinterpret_cast from float? We're pretty much stuck
using binary because of file size. Even using binary files to store the
raw data (floats) some of the files will easily hit 700MB.

Jul 19 '06 #5
Ronin schrieb:
mlimber wrote:
>Ronin wrote:
>>I'm very new to C++ coming in from using C# and the like. I've read a
lot of good posts about binary files but I'm still missing a few key
ingredients that will help me with the following problem.

I have a binary file that has a specific number of floats per row. I
need to read each float into a float array for processing by another
block of code (calculations). Each row gets read into this array and
processed before the next row gets read.

How do I read in from a binary file and store into a float array?
How do I keep track of the position I'm at in the file so I can process
each row individually?
Use std::ifstream::read(), not the extraction operator (>>). You'll
need an ugly reinterpret_cast from float, and you'll want to make sure
the binary format for the floats in your file is the same as those for
your program. The binary float representation is not standardized in
C++, and different implementations can do it differently (this is a
good reason to prefer text instead of binary when possible).

Cheers! --M

How do you do a reinterpret_cast from float? We're pretty much stuck
using binary because of file size. Even using binary files to store the
raw data (floats) some of the files will easily hit 700MB.
char raw_data[ sizeof(float) ];
// load data into raw_data
float f = *reinterpret_cast<float*>(raw_data);

It's implementation defined or so.

<Offtopic>
For the file size: You could store it as compressed file (using libz or
libbzip2 or similar).

--
Thomas
Jul 19 '06 #6

Ronin wrote:
mlimber wrote:
Ronin wrote:

Use std::ifstream::read(), not the extraction operator (>>). You'll
need an ugly reinterpret_cast from float, and you'll want to make sure
the binary format for the floats in your file is the same as those for
your program. The binary float representation is not standardized in
C++, and different implementations can do it differently (this is a
good reason to prefer text instead of binary when possible).
How do you do a reinterpret_cast from float? We're pretty much stuck
using binary because of file size. Even using binary files to store the
raw data (floats) some of the files will easily hit 700MB.
float f;
ifstream is("filename");
is.read(reinterpret_cast < char * (&f), sizeof(f));

Jul 19 '06 #7

Thomas J. Gritzan wrote:
>
char raw_data[ sizeof(float) ];
// load data into raw_data
float f = *reinterpret_cast<float*>(raw_data);

It's implementation defined or so.

<Offtopic>
For the file size: You could store it as compressed file (using libz or
libbzip2 or similar).
It's not a flat text file, it's a binary -- the data is stored as a
float an needs to be cast FROM float, not TO float.

Jul 19 '06 #8
sh**********@comcast.net wrote:
Thomas J. Gritzan wrote:
>char raw_data[ sizeof(float) ];
// load data into raw_data
float f = *reinterpret_cast<float*>(raw_data);

It's implementation defined or so.

It's not a flat text file, it's a binary -- the data is stored as a
float an needs to be cast FROM float, not TO float.
Well, once the floats have been written to a file in binary format, then
when you read in the file, they are nothing more than a collection of
bytes. You need to cast it TO float so that the program knows to treat
them as floats.

It's like:

float ==(convert to binary)==11011001 ==(write to file)==file

float gets converted into binary format and then written to the file.

So when reading, you have to do:

file ==(read from file)==11011001 ==(convert back to float)==float

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 19 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Eric | last post: by
9 posts views Thread by Ching-Lung | last post: by
8 posts views Thread by dagecko | last post: by
10 posts views Thread by joelagnel | last post: by
3 posts views Thread by nicolasg | last post: by
15 posts views Thread by JoeC | last post: by
3 posts views Thread by masood.iqbal | last post: by
9 posts views Thread by deepakvsoni | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.