二进制打开文件VS文本之间的差异 [英] Difference between opening a file in binary vs text

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

问题描述

我已经做了一些东西,如:

I've done some stuff like:

FILE* a = fopen("a.txt", "w");
const char* data = "abc123";
fwrite(data, 6, 1, a);
fclose(a);

,然后在生成的文本文件,它说:ABC123一样的预期。但后来我做的:

and then in the generated text file, it says "abc123" just like expected. But then I do:

//this time it is "wb" not just "w"
FILE* a = fopen("a.txt", "wb");
const char* data = "abc123";
fwrite(data, 6, 1, a);
fclose(a);

和得到完全相同的结果。如果我读使用二进制或正常模式下的文件,这也给了我同样的结果。所以我的问题是,什么是有或没有二进制模式fopening之间的区别。

and get the exact same result. If I read the file using binary or normal mode, it also gives me the same result. So my question is, what is the difference between fopening with or without binary mode.

我在哪里读到的fopen模式: http://www.cplusplus.com/reference/ cstdio / FOPEN /

Where I read about fopen modes: http://www.cplusplus.com/reference/cstdio/fopen/

推荐答案

你给不实际描述的差异,但它埋在页面底部的链接:

The link you gave does actually describe the differences, but it's buried at the bottom of the page:

http://www.cplusplus.com/reference/cstdio/fopen/

文本文件包含文本行的序列文件。 根据其中应用程序运行时,可以在输入/输出操作发生在文本模式下一些特殊字符转换使其适应系统特定的文本文件格式的环境。虽然某些环境没有发生转换和两个文本文件和二进制文件将被视为相同的方式,使用适当的模式提高了便携性。

Text files are files containing sequences of lines of text. Depending on the environment where the application runs, some special character conversion may occur in input/output operations in text mode to adapt them to a system-specific text file format. Although on some environments no conversions occur and both text files and binary files are treated the same way, using the appropriate mode improves portability.

转换可能会恢复正常 \\ r \\ n \\ n (反之亦然),或也许忽略超出0x7F的(一拉在FTP文本模式)字符。我个人倒在二进制模式下打开一切,用好文本编码库中处理文本。

The conversion could be to normalize \r\n to \n (or vice-versa), or maybe ignoring characters beyond 0x7F (a-la 'text mode' in FTP). Personally I'd open everything in binary-mode and use a good text-encoding library for dealing with text.

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

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