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 immediatelyfflush(stdout);fprintf(stderr, "This is to stderr. ");// No need to flush stderr since it is un-bufferedfprintf(stdout, "This is also to stdout. ");// Flush the contents of the output stream immediatelyfflush(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)

  • StackOverflow question on using fflush() in C关于在C中使用fflush()的StackOverflow问题


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

c语言中fflush

c语言中fflush_在C中使用fflush()相关推荐

  1. c语言中math的库函数,C语言中math.h库中的常用函数

    C语言中math.h库中的常用函数 int abs(int i) 返回整型参数i的绝对值 double cabs(struct complex znum) 返回复数znum的绝对值 double fa ...

  2. main c语言中变量的定义,C语言中在main函数中定义的变量是全局变量么_后端开发...

    PHP 和 JavaSript 区别_后端开发 PHP是一种创建动态交互性站点的强有力的服务器端脚本语言,主要用于Web开发领域,而JavaSript是一种具有函数优先的轻量级,解释型或即时编译型的高 ...

  3. c语言中struct和c++中class实例对比

    前言 实现游戏中简单的打怪升级的功能 c语言中的struct #include <stdio.h>typedef void(*Train)(struct player*, int); ty ...

  4. html语言中,定义文档中一个正在打开的链接的颜色的代码是,2017微软认证考试精选练习(附答案)...

    2017微软认证考试精选练习(附答案) 1.如果要使图像在缩放时不失真,在图像显示原始大小时,按下( )键,拖动 图像右下方 的控制点,可以按比例调整图像大小 B A. Ctrl B. Shift C ...

  5. C语言switch中break的作用,C语言中switch...case语句中break的重要性

    在C语言中switch...case语句是经常用到的,下面我介绍一下在使用该语句时候需要注意的一个细节问题.话不多说,直接举例子: 例子1: switch(fruit) { case 1:printf ...

  6. r语言中paste函数_R中的paste()函数-简要指南

    r语言中paste函数 Using the paste() function in R will be straight and simple. In this tutorial let's see ...

  7. java去哪导包_在Java语言中,哪一个包中的类是自动导入的?( )java.applet

    包括,语言中标的详细评审核心是评,标书性审进行查是对实质. 对一的某栋厂行评企业估房进,包中在资估中产评,属于. 完成修正总概算,类动导现工目的会发和数化可能建设.类动导结规模构.程项类型量有设备所变 ...

  8. C语言中sizeof测量形参中数组的长度

    在C语言中,若我们在主函数中定义了一个数组,并给数组赋予了初值,在之后,若有函数需要调用,且需要用到该数组的长度,该怎么来操作. 在下面的代码中定义了一个数组,在main函数中用sizeof函数来计算 ...

  9. python语言中mod_mod在python中怎么用

    MOD是取模运算符. 语法 MOD ( a, b) 通常情况下取模运算(mod)和求余(rem)运算被混为一谈,因为在大多数的编程语言里,都用'%'符号表示取模或者求余运算.在这里要提醒大家要十分注意 ...

最新文章

  1. .NET4.0并行计算技术基础(8)
  2. ikbc键盘自动打字_键盘按斤卖,一斤一百块?IKBC W200机械键盘简晒
  3. python百度云资源-Python开发视频百度云分享
  4. 使用 UIWebView 来播放视频
  5. html 循环tr只显示一个,动态加载进来的tr该如何去循环查看它的值呢
  6. 前端学习(3288):react hook state-hook
  7. 查看Linux系统运行状态(命令汇总)
  8. 大咖布道丨证券行业规模化敏捷和核心能力演进
  9. 用Instant client批量安装Oracle客户端-安装配置
  10. nyoj-488 素数环 +nyoj -32 组合数 (搜索)
  11. 三菱GXWorks2 程序仿真功能
  12. iphone越狱-------平刷回越狱前(未越狱)状态
  13. Linux 虚拟机忘记密码解决办法
  14. 获取openid失败怎么办_微信小程序openid怎么获取 获取微信openid失败解决方法
  15. js中this的作用域
  16. QT串口助手(五):文件操作
  17. 利用手机距离感应器来切换扬声器和听筒播放
  18. 夜神模拟器连接手柄无反应_夜神模拟器手柄设置
  19. 人们为什么会相信伪科学?人类喜欢依赖心理捷径
  20. 爱因斯坦的逻辑思维题

热门文章

  1. Matlab基本数学应用
  2. Python写入文件,但是发现文件为空,竟然未写入!
  3. Arrays.asList 方法注意事项
  4. [转载] Python3 日历(Calendar)模块介绍
  5. MySQL中boolean类型设置
  6. 访问vector元素方法的效率比较(转)
  7. 在tomcat下利用service.xml对项目进行映射部署
  8. 带你玩转JavaWeb开发之四 -如何用JS做登录注册页面校验
  9. 定制属于自己的自动化安装的linux系统镜像
  10. Gym 100818I Olympic Parade(位运算)