[英]Buffer flushing not working for printf function
Coming from here: https://stackoverflow.com/a/1716621/1461017 来自这里: https : //stackoverflow.com/a/1716621/1461017
I want to print separate points (dots, "."), one at a time on the same line, under control of a for
loop: 我想在for
循环的控制下,在同一行上一次打印一个单独的点(点,“。”):
for (i=0; i<100; i++)
{
printf(".");
sleep(1);
}
printf("\n");
But the output only goes to screen after the final "\\n", and I want each one to display immediately upon printing, so as to serve as progress indicators. 但是输出仅在最后一个“ \\ n”之后才显示在屏幕上,我希望每个人在打印后立即显示它们,以便用作进度指示器。 I expected that fflush()
ing the standard output on each loop iteration would produce that result, but this does not work (results are only shown on screen after the execution of the for loop): 我期望fflush()
在每次循环迭代中使用标准输出会产生该结果,但这不起作用(结果仅在执行for循环后显示在屏幕上):
for (i=0; i<100; i++)
{
printf(".");
sleep(1);
fflush(stdout);
}
printf("\n");
I have furthermore tested all of these approaches, both in and out of the loop, with no success: 我进一步测试了所有这些方法,无论是循环内还是循环外,都没有成功:
fflush(stdout);
fflush(NULL);
setbuf(stdout,NULL);
What is going on here, and how can I solve it? 这是怎么回事,我该如何解决?
Further Data: 进一步数据:
Well... a stupid issue, but I think I should write it down anyway, because someone could have it too: 好吧……这是一个愚蠢的问题,但我想无论如何我都应该写下来,因为有人也可以这样做:
The problem was: on the command-line testing, I appended some | more
问题是:在命令行测试中,我附加了| more
| more
in order to stop the screen dumping. | more
以停止屏幕倾倒。 So the stdout
buffer was altered by the piping |
因此, stdout
缓冲区被管道更改了|
command. 命令。
Say my program binary file was named dotting
. 假设我的程序二进制文件名为dotting
。 I called it by typing: 我通过键入以下内容来调用它:
dotting | more
I hope this ridiculous case could help anyone in a future. 我希望这个荒谬的案例将来能对任何人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.