我在 cygwin/gcc 4.7.3 和 VS 2010 下运行了以下程序。
#include <stdio.h>
int main()
{
printf("This is a test.\n");
return 0;
}
在这些环境下运行程序的输出显示 VS 2010 将 stdout
视为文本流。
问题:
stdout
必须是文本流吗?- 实现是否可以自由选择是将文本流还是二进制流用于
stdout
?
编辑
如何以二进制方式写入stdout
的问题与要求stdout
是文本流还是二进制流是不同的。
最佳答案
stdout
必须是文本流,stdin
和 stderr
也是如此。来自C99标准,7.19.3(7):
At program startup, three text streams are predefined and need not be opened explicitly -- standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). (...)
(强调我的)1
供引用:在 2011 年的 C 标准修订版中,这已被原封不动地移动到 7.21.3 (7).2
请注意,这意味着这三个流是文本流在启动时。一些平台提供了稍后切换模式的方式,例如_setmode
在 Windows 下。
1 只是为了完整性:stdin
、stdout
和 stderr
指的是这些流定义见7.19.1(3)。
2 脚注1中提到的部分在C11中移至7.21.1(3)。
关于c - stdout 必须是文本流吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29354228/