从标准输入读取会刷新标准输出吗?

发布于 2024-08-18 12:35:26 字数 646 浏览 11 评论 0原文

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

它以这种方式工作确实有意义,否则像这样的代码:

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

将需要额外的 fflush(stdout);

那么 stdout 是否保证在这里被刷新?

编辑:

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

输入和输出动态 应有互动装置 如 7.19.3 中规定。的意图 这些要求是无缓冲的 或行缓冲输出尽快出现 尽可能地确保提示 消息实际上出现在 程序等待输入。

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

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

stdout is line-buffered when connected to a terminal, but I remember reading somewhere that reading (at least from stdin) will automatically flush stdout. All C implementations that I have used have done this, but I can't find it in the standard now.

It does make sense that it works that way, otherwise code like this:

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

would need an extra fflush(stdout);

So is stdout guaranteed to be flushed here?

EDIT:

As several replies have said, there seems to be no guarantee in the standard that the output to stdout in my example will appear before the read from stdin, but on the other hand, this intent is stated in (my free draft copy of) the standard:

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 Committee Draft -- May 6, 2005, page 14).

So it seems that there is no guarantee, but it will probably work in most implementations anyway. (Famous last words...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

A君 2024-08-25 12:35:26

不,事实并非如此。

No, it does not.

三生池水覆流年 2024-08-25 12:35:26

要回答您的问题,您确实需要在 printf() 调用之后额外的 fflush(stdout); 以确保提示出现在您的程序尝试读取输入。从 stdin 读取并不适合您 fflush(stdout);

To answer your question, you do need the extra fflush(stdout); after your printf() call to make sure the prompt appears before your program tries to read input. Reading from stdin doesn't fflush(stdout); for you.

你丑哭了我 2024-08-25 12:35:26

不,你需要 fflush(stdout);许多实现将在将输出发送到终端的每个换行符处刷新。

No. You need to fflush(stdout); Many implementations will flush at every newline of they are sending output to a terminal.

离线来电— 2024-08-25 12:35:26

否。stdin/stdout 已缓冲。您需要显式地 fflush(stdout) 以便将视频内存/unix 终端内存中的缓冲数据推送到查看设备(例如终端)。数据的缓冲可以通过调用setvbuf来设置。

编辑:谢谢乔纳森,回答这个问题,从标准输入读取不会刷新标准输出。我可能已经偏离了主题,因为我指定了演示如何使用 setvbuf 的代码。

  #include 

  int main(void)
  {
     FILE *input, *output;
     char bufr[512];

     input = fopen("file.in", "r+b");
     output = fopen("file.out", "w");

     /* set up input stream for minimal disk access,
        using our own character buffer */
     if (setvbuf(input, bufr, _IOFBF, 512) != 0)
        printf("failed to set up buffer for input file\n");
     else
        printf("buffer set up for input file\n");

     /* set up output stream for line buffering using space that
        will be obtained through an indirect call to malloc */
     if (setvbuf(output, NULL, _IOLBF, 132) != 0)
        printf("failed to set up buffer for output file\n");
     else
        printf("buffer set up for output file\n");

     /* perform file I/O here */

     /* close files */
     fclose(input);
     fclose(output);
     return 0;
  }

希望这有帮助,
此致,
汤姆.

No. stdin/stdout are buffered. You need to explicity fflush(stdout) in order for the buffered data in the video memory/unix terminal's memory to be pushed out on to a view device such as a terminal. The buffering of the data can be set by calling setvbuf.

Edit: Thanks Jonathan, to answer the question, reading from stdin does not flush stdout. I may have gone off a tangent here by specifying the code demonstrating how to use setvbuf.

  #include 

  int main(void)
  {
     FILE *input, *output;
     char bufr[512];

     input = fopen("file.in", "r+b");
     output = fopen("file.out", "w");

     /* set up input stream for minimal disk access,
        using our own character buffer */
     if (setvbuf(input, bufr, _IOFBF, 512) != 0)
        printf("failed to set up buffer for input file\n");
     else
        printf("buffer set up for input file\n");

     /* set up output stream for line buffering using space that
        will be obtained through an indirect call to malloc */
     if (setvbuf(output, NULL, _IOLBF, 132) != 0)
        printf("failed to set up buffer for output file\n");
     else
        printf("buffer set up for output file\n");

     /* perform file I/O here */

     /* close files */
     fclose(input);
     fclose(output);
     return 0;
  }

Hope this helps,
Best regards,
Tom.

计㈡愣 2024-08-25 12:35:26

不,这不是标准的一部分。当然有可能您使用的库实现确实发生了您所描述的行为,但这是您不应该依赖的非标准扩展。

No, that's not part of the standard. It's certainly possible that you've used a library implementation where the behavior you described did happen, but that's a non-standard extension that you shouldn't rely on.

私野 2024-08-25 12:35:26

否。在处理 std 流时,无论是在 stdin 上读取还是在 stdout 块上写入,请注意进程间死锁。

No. Watch out for inter-process deadlocks when dealing with std streams when either read on stdin or write on stdout blocks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文