std :: ofstream :: write添加字符 [英] std::ofstream::write adds characters
本文介绍了std :: ofstream :: write添加字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用std::ofstream::write
方法写入二进制文件.我发现有些字符不是按原样书写的,例如:
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;
会将以下内容写入二进制文件:0d 0a 00 00 00
为什么还会出现其他0d
字节?
will write the following into a binary file: 0d 0a 00 00 00
Why is there additional 0d
byte appearing?
推荐答案
打开时,您必须指定std::ofstream::binary
.
You´ll have to specify std::ofstream::binary
when opening.
否则,在Windows的文本文件模式下,在写入/读取文件时,程序中的\n
(0x0a
)将被转换为\r\n
(0x0d 0x0a
).
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.
这篇关于std :: ofstream :: write添加字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文