简体   繁体   English

C ++ ifstream,ofstream:原始read()/ write()调用和二进制模式下的打开文件有什么区别?

[英]C++ ifstream, ofstream: What's the difference between raw read()/write() calls and opening file in binary mode?

This question concerns the behaviour of ifstream and ofstream when reading and writing data to files. 这个问题涉及在向文件读取和写入数据时ifstream和ofstream的行为。

From reading around stackoverflow.com I have managed to find out that operator<< (stream insertion operator) converts objects such as doubles to text representation before output, and calls to read() and write() read and write raw data as it is stored in memory (binary format) respectively. 从阅读stackoverflow.com我已经设法找出operator<< (流插入运算符)在输出之前将诸如双精度的对象转换为文本表示,并且调用read()write()读取和写入原始数据,因为它是分别存储在存储器(二进制格式)中。 EDIT: This much is obvious, nothing unexpected here. 编辑:这很明显,这里没什么意外。

I also found out that opening a file in binary mode prevents automatic translation of newline characters as required by different operating systems. 我还发现以二进​​制模式打开文件会阻止不同操作系统根据需要自动转换换行符。

So my question is this: Does this automatic translation, eg; 所以我的问题是:这是自动翻译,例如; from \\n to \\r\\n occur when calling functions read() and write() ? 在调用函数read()write()时从\\n\\r\\n发生? Or is this behaviour just specific to the operator<< . 或者这种行为仅适​​用于operator<< (And also operator>> .) (还有operator>> 。)

Note there is a similar but slightly less specific question here. 请注意,这里有一个类似但稍微不那么具体的问题。 It does not give a definite answer. 它没有给出明确的答案。 Difference in using read/write when stream is opened with/without ios::binary mode 使用/不使用ios :: binary模式打开流时使用读/写的区别

The difference between binary and text mode its at a lower level. 二进制和文本模式之间的差异在较低的水平。

If you open a file in text mode you will get translated data even when using read and write operations. 如果您在文本模式下打开一个文件,你甚至会在使用会转换数据readwrite操作。

Please also note that you're allowed to seek to a position in a text file only if the position was obtained from a previous tell (or 0). 另请注意,只有当位置是从先前的tell (或0)获得时,才允许您在文本文件中seek职位。 To be able to do random positioning, the file must have been opened in binary mode. 为了能够进行随机定位,必须以二进制模式打开文件。

Short answer -- no translation done when using read() & write(). 简短回答 - 使用read()和write()时没有完成翻译。 [The answer to your question is "no."] [你的问题的答案是“不。”]

Longer answer -- read() & write() operate in binary mode which means that the contents are considered to be "binary data." 更长的答案 - read()和write()以二进制模式运行,这意味着内容被认为是“二进制数据”。 A \\n is an ASCII 10 and 10 is a legitimate data value that could, for instance, represent the number 10. A \\ n是ASCII 10,10是合法数据值,例如,可以表示数字10。

This business of changing \\n to \\r\\n is a Windows issue. 将\\ n更改为\\ r \\ n的业务是Windows问题。 In Linux, end of line is marked simply with \\n and no translation is needed. 在Linux中,行尾仅用\\ n标记,不需要翻译。

If you look at the manual page for fopen at http://linux.die.net/man/3/fopen there's this paragraph 如果你看一下http://linux.die.net/man/3/fopen上fopen的手册页,那就是这一段

The mode string can also include the letter 'b' either as a last character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with C89 and has no effect; the 'b' is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary files differently, and adding the 'b' may be a good idea if you do I/O to a binary file and expect that your program may be ported to non-UNIX environments.)

Hopefully that will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM