Difference between int fpurge () and int fflush () in C

Can someone explain me the difference between fpurge(FILE *stream) and fflush(FILE *stream) in C? Both fflush() and fpurge() will discard any unwritten or unread data in the buffer. Please explain to me the exact difference between the two, as well as their pros and cons.

+6
source share
2 answers

"... both fflush and fpurge will discard any unwritten or unread data in the buffer ...": None.

  • fflush :

    The fflush function forces all buffered data to be written for a given output or update stream through the write function underlying the stream. The open status of the stream does not change. If the stream argument is NULL , fflush clears all open output streams.

  • fpurge :

    The fpurge function erases any input or output buffered in this stream. For output streams, this discards any unwritten output. For input streams, this discards any input read from the base object but not yet received using getc . This includes any text discarded using ungetc . (PS: there is also __fpurge , which does the same, but returns no value).

In addition to the obvious effect on buffered data, use wherever you notice the difference with input streams. You can fpurge one such stream (although this is usually a mistake, perhaps a conceptual one). Depending on the environment, you may not fflush input stream (its behavior may be undefined, see the man page ). In addition to the above differences: 1) the cases where they lead to errors are different, and 2) fflush can work in all output streams with one statement, as said (this can be very useful).

As for the pros and cons, I would not quote any at all ... they just work different (mostly), so you should know when to use them.

In addition to the functional difference (what you requested) there is a portability difference: fflush is a standard function, and fpurge is not (and __fpurge too).

Here you have the relevant manual pages ( fflush , fpurge ).

+4
source

To begin with, both functions clear buffers (the type of operational buffers is discussed below), the main difference is what happens to the data present in the buffer.

  • For fflush() data is forced to be written to disk.
  • For fpurge() data is discarded.

Thus, fflush() is the standard C function mentioned in chapter C11 , ยง7.21.5.2.

While fpurge() is a non-portable and non-standard function. On the page

These functions are non-standard and not portable. The fpurge() function was introduced in 4.4BSD and is not available on Linux. The __fpurge() function was introduced in Solaris and is present in glibc 2.1.95 and later.

However, the main difference in use,

  • Calling fflush() with input undefined behavior .

    If stream indicates an output stream or an update stream in which the last operation has not been entered, the fflush function calls any unwritten data for this stream for delivery to the host environment, which must be written to a file; otherwise, the behavior is undefined.

  • A call to fpurge() with an input stream is defined.

    For input streams, this discards any input read from the base object but not yet received via getc(3) ; this includes any text discarded through ungetc(3) .

Try to stick with fflush() still.

+2
source

All Articles