以二进制和文本模式编写的文件之间的区别 [英] Difference between files written in binary and text mode

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

问题描述

写入在文本模式下打开但在二进制模式下不会打开的文件时,会发生什么翻译?

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;

二进制模式:

FILE *fp_binary = fopen(filename, "wb");
fwrite(buffer, size, count, fp_binary);

对文本模式:

FILE *fp_text = fopen(filename, "wt");
fwrite(buffer, size, count, fp_text);


推荐答案

我相信大多数平台都会忽略 t选项或文本模式选项。但是,在Windows上并非如此。如果您在以下位置查看fopen()函数的描述: MSDN ,您将看到指定 t选项将具有以下效果:

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:


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

  • 回车/换行符序列将在输入时转换为换行符。

  • 如果以附加模式打开文件,则将检查文件末尾是否有ctrl-z字符(字符26),并删除该字符(如果可能),还将解释该字符的存在。字符是文件的结尾。这是CPM以来的不幸保留(关于父母在孩子到第三代或第四代时所犯下的罪行),与先前所说的ctrl-z相反字符将不会被追加。

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

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