使用C中的暂停在同一行中打印

时间:2022-10-25 00:07:42

I want to make my program to print something, then wait for a few seconds and then print something else in the same line. I've tried to write it as:

我想让我的程序打印一些东西,然后等待几秒钟然后在同一行中打印其他东西。我试着把它写成:

printf ("bla bla bla");
sleep (2);
printf ("yada yada yada\n");

but in the output I get to wait for 2 seconds and then I get the whole line printed as one. When I tried to put the output in different lines it did print with a pause.

但在输出中我会等待2秒然后我将整行打印为一行。当我尝试将输出放在不同的行中时,它会暂停打印。

How do I make it to print with a pause in the same line?

如何在同一行中暂停打印?

*Working on Linux

*在Linux上工作

2 个解决方案

#1


17  

printf ("bla bla bla");
fflush(stdout);
sleep (2);
printf ("yada yada yada\n");

fflush forces the stdout internal buffer to be flushed to the screen.

fflush强制将stdout内部缓冲区刷新到屏幕。

#2


3  

The stdout is a line buffered stream by default, this means you need to explicitly flush it. It is implicitly flushed on newline. This behavior is mandated by the C99 standard.

默认情况下,stdout是行缓冲流,这意味着您需要显式刷新它。它在换行符上隐式刷新。此行为是C99标准强制执行的。

This means in your first printf, the text is added to an internal buffer. This is done to improve efficiency, e.g. when printing lots of small text fragments.

这意味着在您的第一个printf中,文本被添加到内部缓冲区。这样做是为了提高效率,例如当打印大量的小文本片段时。

Your second printf contains a newline, and that causes the stream to get flushed. You can explicitly flush stdout via fflush(stdout); if you want.

您的第二个printf包含换行符,这会导致流被刷新。你可以通过fflush(stdout)显式刷新stdout;如果你想。

As an alternative you could also use the unbuffered stderr, as in fprintf(stderr, "bla bla bla");, but as its name implies it is intended for errors and warnings.

作为替代方案,您也可以使用无缓冲的stderr,如fprintf(stderr,“bla bla bla”);,但正如其名称所暗示的那样,它用于错误和警告。

See also the SO question Why does printf not flush after the call unless a newline is in the format string?.

另请参阅SO问题为什么printf在调用后不会刷新,除非换行符在格式字符串中?

#1


17  

printf ("bla bla bla");
fflush(stdout);
sleep (2);
printf ("yada yada yada\n");

fflush forces the stdout internal buffer to be flushed to the screen.

fflush强制将stdout内部缓冲区刷新到屏幕。

#2


3  

The stdout is a line buffered stream by default, this means you need to explicitly flush it. It is implicitly flushed on newline. This behavior is mandated by the C99 standard.

默认情况下,stdout是行缓冲流,这意味着您需要显式刷新它。它在换行符上隐式刷新。此行为是C99标准强制执行的。

This means in your first printf, the text is added to an internal buffer. This is done to improve efficiency, e.g. when printing lots of small text fragments.

这意味着在您的第一个printf中,文本被添加到内部缓冲区。这样做是为了提高效率,例如当打印大量的小文本片段时。

Your second printf contains a newline, and that causes the stream to get flushed. You can explicitly flush stdout via fflush(stdout); if you want.

您的第二个printf包含换行符,这会导致流被刷新。你可以通过fflush(stdout)显式刷新stdout;如果你想。

As an alternative you could also use the unbuffered stderr, as in fprintf(stderr, "bla bla bla");, but as its name implies it is intended for errors and warnings.

作为替代方案,您也可以使用无缓冲的stderr,如fprintf(stderr,“bla bla bla”);,但正如其名称所暗示的那样,它用于错误和警告。

See also the SO question Why does printf not flush after the call unless a newline is in the format string?.

另请参阅SO问题为什么printf在调用后不会刷新,除非换行符在格式字符串中?