我有这个代码:
#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}
输出是hello-err
hello-err
hello-err
hello-err
hello -err
你好-err
以 1 秒的间隔。
我想知道为什么 hello-out 永远不会被打印出来。
最佳答案
您需要 fflush
stdout
因为通常 stdout
是行缓冲的,您不会在程序中发出换行符。
fprintf(stdout,"hello-out");
fflush(stdout);
stderr
默认情况下未完全缓冲,因此您不需要fflush
。
关于c - 为什么在使用 fprintf 时不会打印到标准输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11314825/