c - C 中 int fpurge() 和 int fflush() 的区别

标签 c filestream file-handling fflush

谁能解释一下 C 语言中 fpurge(FILE *stream)fflush(FILE *stream) 之间的区别? fflush()fpurge() 都会丢弃缓冲区中任何未写入或未读取的数据。 请解释一下这两者之间的确切区别以及它们的优缺点。

最佳答案

“... fflushfpurge 都会丢弃缓冲区中任何未写入或未读取的数据...”:否。

  • fflush:

    The function fflush forces a write of all buffered data for the given output or update stream via the stream's underlying write function. The open status of the stream is unaffected. If the stream argument is NULL, fflush flushes all open output streams.

  • fpurge:

    The function fpurge erases any input or output buffered in the given stream. For output streams this discards any unwritten output. For input streams this discards any input read from the underlying object but not yet obtained via getc. This includes any text pushed back via ungetc. (P.S.: there also exists __fpurge, which does the same, but without returning any value).

除了明显的对缓冲数据的影响之外,您会注意到差异的一种用途是输入流。您可以fpurge 一个这样的流(尽管这通常是一个错误,可能是概念上的)。根据环境,您可能不会 fflush 输入流(其行为可能未定义,请参阅 man page )。除了上述差异之外:1) 它们导致错误 的情况不同,以及 2) fflush 可以使用单个语句处理所有输出流,如说(这可能非常有用)。

至于优点和缺点,我不会真正引用任何...它们只是工作方式不同(大部分),所以您应该知道何时使用它们。

除了功能的区别(你问的是什么),还有一个可移植性的区别:fflush 是一个标准函数,而fpurge 不是(__fpurge 也不是)。

这里有相应的手册页(fflushfpurge)。

关于c - C 中 int fpurge() 和 int fflush() 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37537479/

相关文章:

java - 将汉字从一个文件写入另一个文件

java - 如何将图像(应该是精确大小)写入外部存储

C++ 文件处理,如何实现在此代码中找不到的记录

无法访问单独文件夹中的头文件

c - 为什么我得到的是 Make : Circular Dependency Dropped warning?

c++ - 在函数中将数组作为参数传递

c# - 使用 C# 读取文件的创建最后修改时间戳

c - 简单的 C 程序会被编译为 Microsoft 的中间运行时语言还是 Windows 中的机器代码

C++:读取.txt内容并存储到二维数组中

c# - 使用 C# 将 MemoryStream 转换为 FileStream?