在C中读取具有错误访问模式的文件
在一个项目中,我使用fopen(p_full_path,“ ab”);
在我开始编写之前,将文件写为二进制。
当我阅读它时,我注意到我这样做的方式是错误的:要打开文件,我使用fopen(p_full_path,“ r+”);
而不是fopen(p_full_path,“ rb+”) );
。
无论如何,文件的内容都是正确的。因此,我不明白模式之间是否有真正的区别r+
和rb+
,我的意思是,如果我不是,因为我不是,因为我不是将文件读为二进制。
阅读 documentation fopen()没有找到答案。
In a project I'm writing a file as binary, using fopen(p_full_path, "ab");
before I start writing it.
When I read it, I noticed I was doing this in a wrong way: to open the file I used fopen(p_full_path, "r+");
instead of fopen(p_full_path, "rb+");
.
The content of the file is correct anyway. So I don't understand if there is a real difference between the modes r+
and rb+
, I mean if there is the possibility of reading the wrong content because I'm not reading the file as binary.
Reading the documentation of fopen()
I didn't find the answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您问题的答案取决于哪个平台您正在使用。
例如,on posix - compliant平台(例如linux),二进制之间没有差异模式和文本模式。
但是,在Microsoft Windows上,情况大不相同:在二进制模式下,线通过
\ r \ r \ n
的字符序列终止(返回运输返回,然后是线条提要),而在文本模式下,它们是仅由\ n
字符终止。这是因为\ r \ n
在文本模式下自动转换为\ n
,而在二进制模式下,未执行转换。同样,在文本模式下,字符代码\ x1a
被解释为文件的末尾,而在二进制模式下,此值没有特殊的含义,并且被视为一个值。The answer to your question depends on which platform you are using.
For example, on POSIX-compliant platforms (such as Linux), there is no difference between binary mode and text mode.
However, on Microsoft Windows, the situation is very different: In binary mode, lines are terminated by character sequences of
\r\n
(carriage return followed by line feed), whereas in text mode, they are only terminated by\n
characters. This is because\r\n
is automatically converted to\n
in text mode, whereas in binary mode, no conversion is performed. Also, in text mode, the character code\x1A
is interpreted as the end of the file, whereas in binary mode, this value has no special meaning and is treated as a value like any other.