为什么fprintf不直接写入文件,除非使用fflush()? [英] Why fprintf doesn't write directly into the file unless fflush() is used?

查看:531
本文介绍了为什么fprintf不直接写入文件,除非使用fflush()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个在文件中写入值的守护进程。我所观察到的是,当我一直写在一个文件上,文件中没有任何可见的东西。另一方面,如果我使用 fflush()方法,那么这些字符在文件中是可见的。为什么fflush()有所作为?

解决方案

因为是缓冲的。这意味着所有写操作都存储在内存中的缓冲区中,直到缓冲区被刷新。对于 printf 和朋友,当它有一个换行符,或者你明确地调用 fflush ,当然如果缓冲区变满。

I have written a daemon that writes a value in a file. What I have observed is that when I keep writing on a file, there is nothing visible in the file. in other hand, If I use fflush() method then the characters are visible in the file. Why fflush() makes a difference?

解决方案

Because it's buffered. That means all writes are stored in a buffer in memory until the buffer is flushed. For printf and friends it's when it has either a newline, or you explicitly call fflush, or of course if the buffer becomes full.

这篇关于为什么fprintf不直接写入文件,除非使用fflush()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆