在二进制和文本模式writen文件之间的差异 [英] Difference between files writen in binary and text mode

查看:107
本文介绍了在二进制和文本模式writen文件之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写的是在不以二进制方式发生文本模式中打开一个文件时,会发生什么翻译?特别是在MS的Visual C

 无符号字符缓冲区[256];
的for(int i = 0; I< 256;我++)缓冲[我] =我;
INT大小= 1;
诠释计数= 256;

二进制模式:

  FILE * fp_binary = FOPEN(文件名,世行);
FWRITE(缓冲区大小,计数,fp_binary);

对战文本模式:

  FILE * fp_text = FOPEN(文件名,重量);
FWRITE(缓冲区大小,计数,fp_text);


解决方案

我相信,在流处理时,很多平台可以忽略T选项或文本模式选项。上的窗口,然而,这不是这种情况。如果你看一看则fopen()函数的描述为: MSDN ,你会看到指定T选项将具有以下效果:


  • 换行符('\\ n')将被转换为'\\ r \\ n输出序列

  • 回车/换行序列将被转换为换行输入。

  • 如果该文件中追加模式打开,文件的末尾将检查一个CTRL-Z字符(字符26)和字符去掉,如果可能的话。它还将间preT该字符的presence为是文件的末尾。这是从CPM(一些有关父母对自己的孩子被访问到第三或第四代的罪)的天不幸缓缴。相反,previously说认为,CTRL-Z字不会被追加。

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.

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);

解决方案

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:

  • 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.

这篇关于在二进制和文本模式writen文件之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆