C++ 使用 ios::binary 或 ios::out 或两者打开文件有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2225600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between opening a file with ios::binary or ios::out or both?
提问by Alan_AI
I'm trying to figure out the difference between opening a file like:
我试图找出打开文件之间的区别,例如:
fstream *fileName*("FILE.dat",ios::binary);
or
或者
fstream *fileName*("FILE.dat",ios::out);
or
或者
fstream *fileName*("FILE.dat",ios::binary | ios::out);
I found that all of these forms are identical: in all cases, the same output on the file is produced using either *fileName*<<or *fileName*.write().
我发现所有这些形式都是相同的:在所有情况下,文件上的相同输出是使用*fileName*<<或 生成的*fileName*.write()。
回答by Nick Bedford
ios::outopens the file for writing.
ios::out打开文件进行写入。
ios::binarymakes sure the data is read or written without translating new line characters to and from \r\non the fly. In other words, exactly what you give the stream is exactly what's written.
ios::binary确保读取或写入数据,而无需\r\n即时转换换行符。换句话说,你给流的正是所写的。
回答by Alan_AI
Opening a file with ios::binary controls how newline characters are handled. On Windows, they are expanded to CRLF pairs. That's it - it has no effect on how things like operator<< work.
使用 ios::binary 打开文件控制换行符的处理方式。在 Windows 上,它们被扩展为 CRLF 对。就是这样 - 它对 operator<< 之类的事情没有影响。

