C++ 以二进制和文本模式编写的文件之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/229924/
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
Difference between files written in binary and text mode
提问by jholl
What translation occurs when writing to a file that was opened in text mode that does not occur in binary mode? Specifically in MS Visual C.
写入在文本模式下打开但在二进制模式下没有发生的文件时会发生什么转换?特别是在 MS Visual C 中。
unsigned char buffer[256];
for (int i = 0; i < 256; i++) buffer[i]=i;
int size = 1;
int count = 256;
Binary mode:
二进制模式:
FILE *fp_binary = fopen(filename, "wb");
fwrite(buffer, size, count, fp_binary);
Versus text mode:
对比文本模式:
FILE *fp_text = fopen(filename, "wt");
fwrite(buffer, size, count, fp_text);
采纳答案by Jon Trauntvein
I believe that most platforms will ignore the "t" option or the "text-mode" option when dealing with streams. On windows, however, this is not the case. If you take a look at the description of the fopen() function at: MSDN, you will see that specifying the "t" option will have the following effect:
我相信大多数平台在处理流时都会忽略“t”选项或“text-mode”选项。然而,在 Windows 上,情况并非如此。如果您在MSDN 上查看fopen() 函数的说明,您将看到指定“t”选项将产生以下效果:
- line feeds ('\n') will be translated to '\r\n" sequences on output
- carriage return/line feed sequences will be translated to line feeds on input.
- If the file is opened in append mode, the end of the file will be examined for a ctrl-z character (character 26) and that character removed, if possible. It will also interpret the presence of that character as being the end of file. This is an unfortunate holdover from the days of CPM (something about the sins of the parents being visited upon their children up to the 3rd or 4th generation). Contrary to previously stated opinion, the ctrl-z character will not be appended.
- 换行符 ('\n') 将在输出时转换为 '\r\n" 序列
- 回车/换行序列将转换为输入的换行符。
- 如果文件以追加模式打开,将检查文件末尾是否有 ctrl-z 字符(字符 26),并在可能的情况下删除该字符。它还将将该字符的存在解释为文件结尾。这是从 CPM 时代(关于父母的罪孽一直延续到他们的孩子直到第三代或第四代)的不幸遗留。与先前陈述的意见相反,不会附加 ctrl-z 字符。
回答by MrZebra
In text mode, a newline "\n" may be converted to a carriage return + newline "\r\n"
在文本模式下,换行符“\n”可能会转为回车+换行符“\r\n”
Usually you'll want to open in binary mode. Trying to read any binary data in text mode won't work, it will be corrupted. You can read text ok in binary mode though - it just won't do automatic translations of "\n" to "\r\n".
通常你会想要以二进制模式打开。尝试以文本模式读取任何二进制数据将不起作用,它会被破坏。不过,您可以在二进制模式下正常阅读文本 - 它不会自动将“\n”转换为“\r\n”。
See fopen
回答by SmacL
Additionally, when you fopen a file with "rt" the input is terminated on a Crtl-Z character.
此外,当您使用“rt”打开文件时,输入会以 Crtl-Z 字符终止。
回答by Ming
Another difference is when using fseek
另一个区别是当使用 fseek
If the stream is open in binary mode, the new position is exactly offset bytes measured from the beginning of the file if origin is SEEK_SET, from the current file position if origin is SEEK_CUR, and from the end of the file if origin is SEEK_END. Some binary streams may not support the SEEK_END.
If the stream is open in text mode, the only supported values for offset are zero (which works with any origin) and a value returned by an earlier call to std::ftell on a stream associated with the same file (which only works with origin of SEEK_SET.
如果流以二进制模式打开,则新位置正好是从文件开头(如果 origin 是 SEEK_SET)、从当前文件位置(如果 origin 是 SEEK_CUR)和从文件末尾(如果 origin 是 SEEK_END)测量的偏移字节。某些二进制流可能不支持 SEEK_END。
如果流在文本模式下打开,则 offset 的唯一支持值是零(适用于任何来源)和较早调用 std::ftell 在与同一文件关联的流上返回的值(仅适用于SEEK_SET 的起源。
回答by Richard Corden
We had an interesting problem with opening files in text mode where the files had a mixture of line ending characters:
我们在以文本模式打开文件时遇到了一个有趣的问题,其中文件混合了行尾字符:
1\n\r
2\n\r
3\n
4\n\r
5\n\r
Our requirement is that we can store our current position in the file (we used fgetpos), close the file and then later to reopen the file and seek to that position (we used fsetpos).
我们的要求是我们可以将当前位置存储在文件中(我们使用 fgetpos),关闭文件,然后重新打开文件并寻找该位置(我们使用 fsetpos)。
However, where a file has mixtures of line endings then this process failed to seek to the actual same position. In our case (our tool parses C++), we were re-reading parts of the file we'd already seen.
但是,如果文件具有混合行尾,则此过程无法找到实际的相同位置。在我们的例子中(我们的工具解析 C++),我们正在重新读取我们已经看到的文件部分。
Go with binary - then you can control exactly what is read and written from the file.
使用二进制 - 然后您可以准确控制从文件中读取和写入的内容。
回答by Ajith Kumat
In 'w' mode, the file is opened in write mode and the basic coding is 'utf-8' in 'wb' mode, the file is opened in write -binary mode and it is resposible for writing other special characters and the encoding may be 'utf-16le' or others
在'w'模式下,文件以写入模式打开,基本编码为'wb'模式下的'utf-8',文件以写入-二进制模式打开,负责写入其他特殊字符和编码可能是“utf-16le”或其他