c语言中fflush_在C中使用fflush()

c语言中fflush

In this article, we’ll take a look at how we can use the fflush() function in C.

在本文中,我们将研究如何在C中使用fflush()函数。

The purpose of this function may not be immediately clear to you, but you might take a hint from the name of the function.

您可能不会立即清楚此功能的用途,但是您可能会从功能名称中得到提示。



C语言中的fflush()函数 (The fflush() function in C)

The fflush() function (Flush to File) is a function which flushes the contents of any output stream (or update stream) to the corresponding file.

fflush()函数(刷新到文件)是将任何输出流 (或更新流)的内容刷新到相应文件的函数。


fflush(file);

Why do we need this function, and not just something like fprintf() or printf()?

为什么我们需要此功能,而不仅仅是fprintf()printf()

The answer is due to how the Operating System handles this input. Whenever we pass any output, the Operating System actually puts the content (which will be eventually written to the output file – like stdout) in a buffer.

答案是由于操作系统如何处理此输入。 无论何时传递任何输出,操作系统实际上都会将内容(最终将写到输出文件中的内容,例如stdout )放在缓冲区中。

This buffer will automatically get flushed at a file only at certain instances.

仅在某些情况下,此缓冲区才会自动刷新到文件。



了解对fflush()的需求 (Understanding the need for fflush())

To illustrate a common issue that we face due to output buffering, consider the following code snippet.

为了说明由于输出缓冲而面临的常见问题,请考虑以下代码片段。


#include <stdio.h>

int main() {
    fprintf(stdout, "This is to stdout. ");
    fprintf(stderr, "This is to stderr. ");
    fprintf(stdout, "This is also to stdout. ");
}

You might expect that the output comes in order, but the reality is a bit different.

您可能期望输出顺序良好,但实际情况有所不同。


This is to stderr. This is to stdout. This is also to stdout. 

Why does this happen? This is because of how the files for stdout and stderr are buffered.

为什么会这样? 这是因为如何缓冲stdoutstderr的文件。

By default, stdout is newline-buffered, while stderr is unbuffered.

默认情况下, stdout换行缓冲的 ,而stderr是未缓冲的

What does this mean? For stdout, until there is a newline, the contents of the output stream are actually stored inside a temporary buffer.

这是什么意思? 对于stdout ,直到出现换行符为止,输出流的内容实际上存储在临时缓冲区内。

Only when the output stream encounters a newline (or end of file), will the contents be written to the stdout file. Notice the lack of a newline in our program!

仅当输出流遇到换行符(或文件末尾)时,内容才会写入stdout文件。 注意我们的程序中缺少换行符!

But, for stderr, the stderr stream is not buffered, so the contents are immediately written to it!

但是,对于stderr ,不会缓存stderr流,因此会将内容立即写入其中!

So, in our example program, since we don’t have a newline, stderr will be written first. Only after the end of file (output) is reached, will the contents of stdout be written!

因此,在示例程序中,由于没有换行符,因此将首先编写stderr 。 只有到达文件末尾(输出)后,才会写入stdout的内容!

So if you want to have stdout output first, without using a newline, you must flush the output buffer using fflush(). Makes sense?

因此,如果要先stdout输出而不使用换行符,则必须使用fflush() 刷新输出缓冲区。 说得通?

Let’s now add fflush() after writing to stdout, so that the contents are instantly written to the output file.

现在让我们在写入stdout之后添加fflush() ,以便将内容立即写入输出文件。


#include <stdio.h>

int main() {
    fprintf(stdout, "This is to stdout. ");
    // Flush the contents of the output stream immediately
    fflush(stdout);
    fprintf(stderr, "This is to stderr. ");
    // No need to flush stderr since it is un-buffered
    fprintf(stdout, "This is also to stdout. ");
    // Flush the contents of the output stream immediately
    fflush(stdout);
}

Output

输出量


This is to stdout. This is to stderr. This is also to stdout. 

Indeed, we have our problem solved for us, using fflush()!

确实,我们使用fflush()为我们解决了问题!



我可以将fflush(stdin)用于输入流吗? (Can I use fflush(stdin) for Input Streams?)

The short answer is: No, don’t ever use this function.

简短的答案是: ,永远不要使用此功能。

The logic of flushing buffered output stream to files makes sense, since we know that the output contents will be eventually be written to a file / stream

将缓冲的输出流刷新到文件的逻辑很有意义,因为我们知道输出内容最终将被写入文件/流

But for input streams, there is no way of knowing where the input will end up eventually. For all you know, there may be some external program which feeds input randomly to this program, and you have no way of knowing what will happen to it!

但是对于输入流 ,无法知道输入最终将在哪里结束。 就您所知,可能会有一些外部程序将输入随机地馈送到该程序,而您却无法知道它将发生什么!

So, code of the form: fflush(stdin) is undefined behavior!!!

因此,形式为fflush(stdin)是未定义的行为!!!



结论 (Conclusion)

In this article, we learned how we could use the fflush() function in C, to flush output buffers to files.

在本文中,我们学习了如何在C中使用fflush()函数将输出缓冲区刷新到文件。

For similar articles, do go through our tutorial section on C programming!

对于类似的文章,请阅读我们有关C编程的教程部分



参考资料 (References)



翻译自: https://www.journaldev.com/39049/fflush-in-c

c语言中fflush

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值