C语言 Linux 中没有 O_BINARY 和 O_TEXT 标志?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2266992/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:29:26  来源:igfitidea点击:

No O_BINARY and O_TEXT flags in Linux?

cfile

提问by Pieter

When using system-level IO in Linux, I noticed that the compiler recognized the O_RDONLYand O_RDWRflags, but it had no clue whatsoever as to the meaning of the O_BINARYand O_TEXTflags.

在 Linux 中使用系统级 IO 时,我注意到编译器识别O_RDONLYO_RDWR标志,但它不知道O_BINARYO_TEXT标志的含义。

Is this a Linux thing?

这是 Linux 的东西吗?

回答by Tim Yates

Linux, and just about every flavor of Unix for that matter, doesn't differentiate between binary and text files. Thus, there are no standard constants with that name. You can manually define the constants to be zero in Linux if you want to include them in your code for portability purposes.

Linux,以及几乎所有风格的 Unix,都不区分二进制文件和文本文件。因此,没有具有该名称的标准常量。如果您想将常量包含在代码中以实现可移植性,您可以在 Linux 中手动将常量定义为零。

http://unix.derkeiler.com/Newsgroups/comp.unix.programmer/2007-03/msg00147.html

http://unix.derkeiler.com/Newsgroups/comp.unix.programmer/2007-03/msg00147.html

回答by AnT

At the level of C language and its standard library, there's no such thing as O_BINARYand O_TEXTflags. The binary or text mode is selected by adding the bspecifier of the mode parameter of fopenfunction. The specifier itself is, of course, supported by all C implementations, but on POSIX platforms this specifier has no effect: per POSIX specification the text mode is the same as the binary mode.

在 C 语言及其标准库的层面上,没有O_BINARYO_TEXT标志这样的东西。通过添加函数b的模式参数说明符来选择二进制或文本模式fopen。当然,所有 C 实现都支持说明符本身,但在 POSIX 平台上,此说明符不起作用:根据 POSIX 规范,文本模式与二进制模式相同。

Not suprisingly, if you dig deeper into the level of non-standard platform-specific Unix I/O functions, you'll discover that they have no knowledge of that text/binary distinction whatsoever.

不足为奇,如果您深入研究非标准平台特定的 Unix I/O 函数的级别,您会发现他们根本不知道文本/二进制区别。

回答by nobody

It's a *nix thing. *nix operating systems don't do automatic linefeed conversion for I/O on "text" files so O_TEXT and O_BINARY flags wouldn't make sense.

这是* nix的事情。*nix 操作系统不对“文本”文件的 I/O 进行自动换行转换,因此 O_TEXT 和 O_BINARY 标志没有意义。

回答by Andrew Aylett

Windows uses \r\n for line endings, Linux (and other Unix-alikes) use just \n. In Windows, reading O_BINARY gives you the raw data, possibly odd line endings and all, while O_TEXT normalises the line endings, so your C code only sees a single character.

Windows 使用 \r\n 作为行尾,Linux(和其他类 Unix 系统)只使用 \n。在 Windows 中,读取 O_BINARY 为您提供原始数据,可能是奇数行结尾和所有内容,而 O_TEXT 使行结尾标准化,因此您的 C 代码只能看到一个字符。

Under Linux et al, there's no point distinguishing between text and binary, because the data only has a single character anyway, so the flags are unnecessary.

在 Linux 等下,没有必要区分文本和二进制,因为数据无论如何都只有一个字符,所以标志是不必要的。

回答by AProgrammer

There isn't a difference at the OS level between binary and text file under Unix. Text file have just a restricted content. That's also true for Windows, but the conventions used by C for the end of lines are the same as the one used by Unix, while Windows use CR/LF pair (and an explicit end of file marker in some contexts, but the handling of that was not consistent even in the system programs last time I checked), so a mapping is needed to respect the conventions mandated by C.

Unix 下的二进制文件和文本文件在操作系统级别没有区别。文本文件只有有限的内容。对于 Windows 也是如此,但是 C 用于行尾的约定与 Unix 使用的约定相同,而 Windows 使用 CR/LF 对(以及在某些上下文中显式的文件结束标记,但处理即使在我上次检查的系统程序中也不一致),因此需要映射以遵守 C 强制要求的约定。