c - 文件定位功能是否会在内部将输出刷新到设备并清除输入缓存?

标签 c linux io

来自 https://stackoverflow.com/a/52068965/156458

When switching between reading and writing, that buffer must be cleared before switching modes, otherwise data loss could potentially occur. Each of the operations mentioned above perform the required flushing of the buffer.

“操作”包括 fseek、fsetpos 和 rewind。

文件定位功能是否会在内部将输出刷新到设备并清除输入缓存? 7.21.5.3p7 in the C standard没说。我也没有在文件定位函数的联机帮助页中找到它。

如果不是,为什么要在输出和输入之间调用文件定位函数(如果不是 fflush)?

谢谢。

最佳答案

用于打开文件的更新 (+) 模式的描述包含以下详细信息:

When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters endof-file.

因此,虽然这些函数的描述没有明确提到它们会刷新缓冲区,但该子句暗示了这一点。但只有在使用更新模式时才需要。

关于c - 文件定位功能是否会在内部将输出刷新到设备并清除输入缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52106265/

相关文章:

c - Fwrite/Fread 动态声明的结构 **

c - 为什么在 fclose 上出现段错误?

c - 使用 gethostbyname_r() 函数要包含哪个头文件

c# - windows服务exe停止后如何修改?

c++ - 将二进制文件读入 Armadillo 矩阵的直接方法?

C 警告(clang 编译器) "integer literal is too large to be represented in a signed integer"

c++ - 对于英语单词来说,什么是好的哈希函数?

linux - 想要用一个命令完成所有行 - IFS

将网页从浏览器转储到图像文件的linux脚本

c# - 在 Windows Phone 中写入文件