Linux fopen 中的 r 和 rb 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2174889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what's the differences between r and rb in fopen
提问by Shaobo Wang
I tried using fopen in C, the second parameter is the open mode. The two modes "r" and "rb" tend to confuse me a lot. It seems they are the same. But sometimes it is better to use "rb". So, why does "r" exist? Explain it to me in detail or with examples. Thank You.
我尝试在 C 中使用 fopen,第二个参数是打开模式。两种模式“r”和“rb”往往让我很困惑。似乎他们是一样的。但有时最好使用“rb”。那么,为什么“r”存在?向我详细解释或举例说明。谢谢你。
采纳答案by caf
You should use "r"
for opening text files. Different operating systems have slightly different ways of storing text, and this will perform the correct translations so that you don't need to know about the idiosyncracies of the local operating system. For example, you will know that newlines will always appear as a simple "\n"
, regardless of where the code runs.
您应该"r"
用于打开文本文件。不同的操作系统存储文本的方式略有不同,这将执行正确的翻译,因此您无需了解本地操作系统的特性。例如,您会知道换行符将始终显示为 simple "\n"
,无论代码在哪里运行。
You should use "rb"
if you're opening non-text files, because in this case, the translations are not appropriate.
"rb"
如果您要打开非文本文件,则应使用,因为在这种情况下,翻译不合适。
回答by sagie
use "rb" to open a binary file. Then the bytes of the file won't be encoded when you read them
使用“rb”打开一个二进制文件。然后文件的字节在您读取它们时不会被编码
回答by Alex Budovski
- "r" is the same as "rt" for Translated mode
- "rb" is non-translated mode.
- “r”与翻译模式的“rt”相同
- “rb”是非翻译模式。
This makes a difference on Windows, at least. See that link for details.
这至少在 Windows 上有所不同。有关详细信息,请参阅该链接。
回答by Alok Singhal
On Linux, and Unix in general, "r"
and "rb"
are the same. More specifically, a FILE
pointer obtained by fopen()
ing a file in in text mode and in binary mode behaves the same way on Unixes. On windows, and in general, on systems that use more than one character to represent "newlines", a file opened in text mode behaves as if all those characters are just one character, '\n'
.
在Linux和Unix在一般情况下,"r"
和"rb"
是相同的。更具体地说,FILE
通过fopen()
在文本模式和二进制模式下 ing 文件获得的指针在 Unix 上的行为方式相同。在 Windows 上,以及一般情况下,在使用多个字符来表示“换行符”的系统上,以文本模式打开的文件的行为就像所有这些字符都只是一个字符'\n'
.
If you want to portably read/write text files on any system, use "r"
, and "w"
in fopen()
. That will guarantee that the files are written and read properly. If you are opening a binary file, use "rb"
and "wb"
, so that an unfortunate newline-translation doesn't mess your data.
如果要在任何系统上可移植地读/写文本文件,请使用"r"
和"w"
in fopen()
。这将保证文件被正确写入和读取。如果您要打开二进制文件,请使用"rb"
and "wb"
,这样不幸的换行转换就不会弄乱您的数据。
Note that a consequence of the underlying system doing the newline translation for you is that you can't determine the number of bytes you can read from a file using fseek(file, 0, SEEK_END).
请注意,底层系统为您进行换行转换的结果是您无法使用fseek(file, 0, SEEK_END)确定可以从文件中读取的字节数。
Finally, see What's the difference between text and binary I/O?on comp.lang.c FAQs.
回答by nathansizemore
On most POSIX systems, it is ignored. But, check your system to be sure.
在大多数 POSIX 系统上,它被忽略。但是,请检查您的系统以确保。
XNU
新疆师范大学
The mode string can also include the letter 'b' either as last character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with ISO/IEC 9899:1990 ('ISO C90') and has no effect; the 'b' is ignored.
模式字符串还可以包括字母“b”作为最后一个字符或作为上述任何两个字符串中的字符之间的字符。这完全是为了与 ISO/IEC 9899:1990 ('ISO C90') 兼容,并且没有任何影响;'b' 被忽略。
Linux
Linux
The mode string can also include the letter 'b' either as a last character or as a character between the characters in any of the two- character strings described above. This is strictly for compatibility with C89 and has no effect; the 'b' is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary files differently, and adding the 'b' may be a good idea if you do I/O to a binary file and expect that your program may be ported to non-UNIX environments.)
模式字符串还可以包括字母“b”作为最后一个字符或作为上述两个字符串中的任何一个中的字符之间的字符。这严格是为了兼容 C89,没有任何影响;'b' 在所有符合 POSIX 的系统上都被忽略,包括 Linux。(其他系统可能会以不同的方式对待文本文件和二进制文件,如果您对二进制文件进行 I/O 操作并希望您的程序可以移植到非 UNIX 环境,则添加“b”可能是个好主意。)