c - 从 stdin 读取是否刷新 stdout?

标签 c stdout stdin flush

stdout 在连接到终端时是行缓冲的,但我记得在某处读到读取(至少从 stdin 读取)会自动刷新 stdout。我用过的所有 C 实现都这样做了,但我现在在标准中找不到它。

它以这种方式工作确实有意义,否则代码如下:

printf("Type some input: ");
fgets(line, sizeof line, stdin);

需要一个额外的fflush(stdout);

那么 stdout 是否保证会在此处刷新?

编辑:

正如几个回复所说,标准中似乎没有保证在我的示例中输出到 stdout 的输出将出现在从 stdin 读取之前,但另一方面,这个意图是在(我的免费草稿副本)标准中说明:

The input and output dynamics of interactive devices shall take place as specified in 7.19.3. The intent of these requirements is that unbuffered or line-buffered output appear as soon as possible, to ensure that prompting messages actually appear prior to a program waiting for input.

(ISO/IEC 9899:TC2 委员会草案 -- 2005 年 5 月 6 日,第 14 页)。

所以看起来并不能保证,但无论如何它可能会在大多数实现中起作用。 (著名的遗言...)

最佳答案

不,它没有。

关于c - 从 stdin 读取是否刷新 stdout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2123528/

相关文章:

macos - 有没有办法在 Mac OS 下将数据发送到另一个进程的标准输入?

python - 在 python 中,如何检查标准输入流 (sys.stdin) 的结尾并对此做一些特殊的事情

android - 禁用 JNI C 代码中变量的编译器优化

指针索引运算符的混淆

c - gdb : address range mappings

python - 如果未提供文件名,则回退到标准输出

c - 某些功能不需要链接

python - 丢弃被测程序的标准输出/标准错误,但保留单元测试输出

c++ - G++ 编译器 : Segfault handling

java - java中STDIN有哪些不同的方式