谁能解释一下 C 语言中 fpurge(FILE *stream)
和 fflush(FILE *stream)
之间的区别?
fflush()
和 fpurge()
都会丢弃缓冲区中任何未写入或未读取的数据。
请解释一下这两者之间的确切区别以及它们的优缺点。
最佳答案
“... fflush
和 fpurge
都会丢弃缓冲区中任何未写入或未读取的数据...”:否。
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 isNULL
,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 viagetc
. This includes any text pushed back viaungetc
. (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
也不是)。
关于c - C 中 int fpurge() 和 int fflush() 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37537479/