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/