使用/不使用ios :: binary模式打开流时使用读/写的区别 [英] Difference in using read/write when stream is opened with/without ios::binary mode
问题描述
在我使用下面的代码片段的实验中,我没有发现任何特殊的区别,无论我创建/没有ios:二进制模式的流:
In my experiments with the following code snippet, I did not find any particular difference whether i created the streams with/without the ios:binary mode:
int main()
{
ifstream ostr("Main.cpp", ios::in | ios::binary | ios::ate);
if (ostr.is_open())
{
int size = ostr.tellg();
char * memBlock = new char[size + 1];
ostr.seekg(0, ios::beg);
ostr.read(memBlock, size);
memBlock[size] = '\0';
ofstream file("trip.cpp", ios::out | ios::binary);
file.write(memBlock, size);
ostr.close();
}
}
这里我试图将原始源文件复制到
Here I am trying to copy the original source file into another file with a different name.
我的问题是当打开fstream对象时,读/写调用(与二进制文件IO相关联)之间的区别是什么有/没有ios ::二进制模式?
使用二进制模式有什么优势吗?当执行文件IO时,何时和何时不使用它?
My question is what is the difference between the read/write calls(which are associated with binary file IO) when an fstream object is opened with/without ios::binary mode ? Is there any advantage of using the binary mode ? when to and when not to use it when doing file IO ?
推荐答案
binary
和 text
模式是如何处理\\\
字符。
The only difference between binary
and text
mode is how the '\n' character is treated.
binary
模式有无翻译。
在文字
模式 \\\
写入
行尾序列
。
在文本
模式结束行序列
翻译成 \\\
。
In text
mode \n
is translated on write into a the end of line sequence
.
In text
mode end of line sequence
is translated on read into \n
.
结束行序列
是平台相关的。
示例:
LF ('\0x0A'): Multics, Mac OS X, BeOS, Amiga, RISC OS
CRLF ('\0x0a\0x0d'): Microsoft Windows, DEC TOPS-10, RT-11
CR: ('\0x0D'): TRS-80, Mac OS Pre X
RS: ('\0x1E'): QNX pre-POSIX implementation.
这篇关于使用/不使用ios :: binary模式打开流时使用读/写的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!