c - 为什么 fopen() 创建的文件流在输入和输出操作之间需要 fflush()?

标签 c linux c99

这是来自 IBM 的引述(C99标准中也有规定)

When you open a file for update, you can perform both input and output operations on the resulting stream. However, an output operation cannot be directly followed by an input operation without an intervening fflush subroutine call or a file positioning operation (fseek, fseeko, fseeko64, fsetpos, fsetpos64 or rewind subroutine). Also, an input operation cannot be directly followed by an output operation without an intervening flush or file positioning operation, unless the input operation encounters the end of the file.

为什么这是必要的?

最佳答案

操作可能会被缓冲以避免将每个字节单独写入磁盘。在开始读取之前,我们必须确保所有先前的写入都实际存储在磁盘上。

此外,无论如何都可能需要搜索来找到您想要阅读的内容。所以在实践中不是问题。

关于c - 为什么 fopen() 创建的文件流在输入和输出操作之间需要 fflush()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45898928/

相关文章:

javascript - 在 c [Codemirror] 中自动完成

linux - 终端给出前缀 : To-be-filled-by-O-E-M

c - 是否有 GCC 关键字允许结构重新排序?

c - 访问不活跃的 union 成员

c - 在可变参数宏中引用特定参数

C - 将二进制转换为十进制的算法 - 不起作用

c - msgget 无法加入 mq

c - 如何在 C 程序中声明、定义和调用函数,该函数必须具有 1 )"fill"3D 数组和 2 )"read"3D 字符数组

linux - 如何使用 zip -T 检查多个 zip 文件的 CRC

c++ - catch-all 处理程序中的 exception_ptr 在 Centos 7 和 Windows 上运行异常