我正在尝试使用std::ofstream::write方法编写二进制文件.我发现,有些字符不是按原样写的,例如:
std::ofstream in("testout");
int i =10;
in.write((const char *)(&i), sizeof(i));
in.close();
return 0;
Run Code Online (Sandbox Code Playgroud)
将以下内容写入二进制文件:0d 0a 00 00 00
为什么会出现其他0d字节?
dev*_*fan 16
你必须std::ofstream::binary在开业时指明.
否则,在Windows上的文本文件模式,\n(0x0a)在程序将从转换为/ \r\n(0x0d 0x0a)写/读文件.