Binary Files

R

Ronin

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
 
O

osmium

Ronin said:
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.
 
M

mlimber

Ronin said:
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
 
R

Ronin

osmium said:
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.
 
R

Ronin

mlimber said:
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.
 
T

Thomas J. Gritzan

Ronin said:
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).
 
S

shadowman615

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));
 
S

shadowman615

Thomas said:
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.
 
M

Marcus Kwok

Thomas said:
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
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top