Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unexplained extra bytes when writing binary data to file

Status
Not open for further replies.

robwil

Technical User
Apr 30, 2004
2
US
Hi all,
I'm trying to write some double precision digits to a file, here's what I'm doing:

Code:
typedef basic_ofstream< double, char_traits<double> > d_ofstream;

d_ofstream myfile("myfile.dat");

double dd[N];

... some processing that fills in the values of dd ...

for(i=0;i<N;i++)
myfile.put(dd[i]); OR myfile << dd[i];

myfile.close();
either of the write instructions gives me the same problem - dd is written to the file, but throughout the file there are unexplained extra pairs of bytes interspersed, specifically "10" "13" (i.e. lf/cr IF we were talking about ASCII). So when I try to read the data in a different application later on it's all screwed up (although if I read back from the file in the same program it does so correctly). I can't figure out what the pattern is to where the bytes appear, but what I expect to be a 1,600,016 byte file ends up as 1,602,774 bytes. Anybody have any ideas? Thanks in advance.
 
You're writing bytes to a text file, which means every byte which corresponds to newline(\n) is translated to \r\n
Which so long as you also read the file as a text file is no problem since the reverse transformation happens (as you observe).

You need to add the 'binary' option flag when you open the file.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top