二进制文件和文本模式之间的区别。

时间:2022-08-29 22:57:21

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.

当写入在文本模式中以二进制模式不出现的文件时,会发生什么翻译?特别是在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);

5 个解决方案

#1


35  

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”选项或“文本模式”选项。然而,在windows上,情况并非如此。如果您查看一下fopen()函数的描述:MSDN,您将会看到指定“t”选项会有以下效果:

  • line feeds ('\n') will be translated to '\r\n" sequences on output
  • line feeds ('\n')将被翻译成'\r\n '序列的输出。
  • 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.
  • 如果该文件在append模式中打开,则将检查该文件的结尾是否为ctrl-z字符(字符26),如果可能的话,删除该字符。它还将解释该字符的存在,作为文件的结尾。这是一个令人遗憾的从CPM时代遗留下来的东西(关于父母的罪行,他们的孩子被带到第三或第四代)。与先前声明的观点相反,ctrl-z字符将不会被追加。

#2


26  

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

看到fopen

#3


5  

Additionally, when you fopen a file with "rt" the input is terminated on a Crtl-Z character.

另外,当您用“rt”打开一个文件时,输入将以Crtl-Z字符结束。

#4


4  

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.

如果流是以二进制模式打开的,则新位置恰好是从文件开始时测量的偏移量,如果源是SEEK_CUR,则从当前的文件位置,如果源是SEEK_END,则从文件的末尾开始。一些二进制流可能不支持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.

如果流在文本模式下是打开的,那么唯一支持的偏移值为0(它可以与任何原点一起工作),并且返回一个之前调用std的值::ftell在与同一文件关联的流上(它只与SEEK_SET的源起作用)。

#5


2  

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.

使用二进制—然后您可以完全控制从文件中读取和写入的内容。

#1


35  

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”选项或“文本模式”选项。然而,在windows上,情况并非如此。如果您查看一下fopen()函数的描述:MSDN,您将会看到指定“t”选项会有以下效果:

  • line feeds ('\n') will be translated to '\r\n" sequences on output
  • line feeds ('\n')将被翻译成'\r\n '序列的输出。
  • 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.
  • 如果该文件在append模式中打开,则将检查该文件的结尾是否为ctrl-z字符(字符26),如果可能的话,删除该字符。它还将解释该字符的存在,作为文件的结尾。这是一个令人遗憾的从CPM时代遗留下来的东西(关于父母的罪行,他们的孩子被带到第三或第四代)。与先前声明的观点相反,ctrl-z字符将不会被追加。

#2


26  

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

看到fopen

#3


5  

Additionally, when you fopen a file with "rt" the input is terminated on a Crtl-Z character.

另外,当您用“rt”打开一个文件时,输入将以Crtl-Z字符结束。

#4


4  

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.

如果流是以二进制模式打开的,则新位置恰好是从文件开始时测量的偏移量,如果源是SEEK_CUR,则从当前的文件位置,如果源是SEEK_END,则从文件的末尾开始。一些二进制流可能不支持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.

如果流在文本模式下是打开的,那么唯一支持的偏移值为0(它可以与任何原点一起工作),并且返回一个之前调用std的值::ftell在与同一文件关联的流上(它只与SEEK_SET的源起作用)。

#5


2  

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.

使用二进制—然后您可以完全控制从文件中读取和写入的内容。