473,386 Members | 1,734 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.

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 4109
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
28
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
8
by: dagecko | last post by:
Hi I would like to know how to detect if a file is binary or not. It's important for me but I don't know where to start. Ty
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
15
by: JoeC | last post by:
I am writing a program that I am trying to learn and save binary files. This is the page I found as a source: http://www.angelfire.com/country/aldev0/cpphowto/cpp_BinaryFileIO.html I have...
3
by: masood.iqbal | last post by:
Hi, Kindly excuse my novice question. In all the literature on ifstream that I have seen, nowhere have I read what happens if you try to read a binary file using the ">>" operator. I ran into...
9
by: deepakvsoni | last post by:
are binary files portable?
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.