fprintf()函数不能将数据保存到文件;必须后跟fflush()函数? [英] The fprintf() function can't persist the data to the file; must be followed by fflush() function?

查看:2053
本文介绍了fprintf()函数不能将数据保存到文件;必须后跟fflush()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 fopen()函数打开一个文件进行写入,并返回一个 FILE 指针。我然后使用这个指针与 fprintf(),但数据不会持久我的写入文件。这是Direct IO / O_DIRECT 相关,还是什么问题?

I am using the fopen() function open a file to write, and return a FILE pointer. I then use this pointer with fprintf(), but data doesn't persist my writes to the file. Is this Direct IO / O_DIRECT related, or what is the problem?

推荐答案

p>您需要致电 fflush() ,或者只是 fclose() 文件。 fclose()将在关闭前尝试刷新。这个行为是ISO C和POSIX的一部分,所以你可以依靠它。有关详细信息,请参见此处 fclose()

You either need to call fflush(), or just fclose() the file when you're done writing to it. fclose() will attempt to flush before closing. This behavior is a part of ISO C and POSIX so you can rely on it. See here for more information about fclose():


刷新流,然后关闭与那
流。然后,
函数释放与流相关的任何缓冲区。冲洗意味着
未写缓冲的数据被写入文件,并且未读缓冲的
数据被丢弃。

Flushes a stream, and then closes the file associated with that stream. Afterwards, the function releases any buffers associated with the stream. To flush means that unwritten buffered data is written to the file, and unread buffered data is discarded.

如果你继续写入一个文件,从不刷新或关闭它,操作系统将开始刷新数据到文件当它准备好,足够的写入排队后。

If you keep writing to a file and never flush or close it, the OS will start flushing data to the file "when it's ready to", after enough writes are queued up.

这篇关于fprintf()函数不能将数据保存到文件;必须后跟fflush()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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