Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::ofstream::write adds characters

Tags:

c++

ofstream

I am trying to write binary file using std::ofstream::write method. I found out, that some characters are not written as they are, for example:

std::ofstream in("testout");
int i =10;

in.write((const char *)(&i), sizeof(i));
in.close();

return 0;

will write the following into a binary file: 0d 0a 00 00 00 Why is there additional 0d byte appearing?

like image 280
coffee Avatar asked Feb 24 '14 08:02

coffee


1 Answers

You´ll have to specify std::ofstream::binary when opening.

Else, on Windows in textfile mode, \n (0x0a) in a program will be converted to/from \r\n (0x0d 0x0a) when writing/reading the file.

like image 98
deviantfan Avatar answered Sep 30 '22 13:09

deviantfan