Can anyone please explain me about 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 me the exact difference between these two and also their pros and cons.
The function fflush(stdin) is used to flush the output buffer of the stream. It returns zero, if successful otherwise, returns EOF and feof error indicator is set.
The _fpurge() function requests that any pending data in the stream be discarded. After a call to __fpurge(), if stream is currently reading, any data that has been read from the system but not yet presented to the application will be discarded.
The fflush function in C is used to immediately flush out the contents of an output stream. This is particularly useful in displaying output, as the operating system may initially put the output data in a temporary buffer before writing it to an output stream or file like stdout .
The function fflush(stdin) is used to flush or clear the output buffer of the stream. When it is used after the scanf(), it flushes the input buffer also. It returns zero if successful, otherwise returns EOF and feof error indicator is set.
"... both fflush
and fpurge
will discard any unwritten or unread data in the buffer..." : No.
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).
Besides the obvious effect on buffered data, one use where you would notice the difference is with input streams. You can fpurge
one such stream (although it is usually a mistake, possibly conceptual). Depending on the environment, you might not fflush
an input stream (its behaviour might be undefined, see man page). In addition to the above remarked differences: 1) the cases where they lead to errors are different, and 2) fflush
can work on all output streams with a single statement, as said (this might be very useful).
As for pros and cons, I would not really quote any... they simply work different (mostly), so you should know when to use each.
In addition to the functional difference (what you were asking), there is a portability difference: fflush
is a standard function, while fpurge
is not (and __fpurge
is not either).
Here you have the respective man pages (fflush
, fpurge
).
To start with, both the functions clear the buffers (type of operable buffers are discussed below), the major difference is what happens with the data present in the buffer.
fflush()
, the data is forced to be written to disk.fpurge()
, data is discarded.That being said, fflush()
is a standard C function, mentioned in the C11
, chapter §7.21.5.2.
Whereas, fpurge()
is a non-portable and non-standard function. From the man page
These functions are nonstandard and not portable. The function
fpurge()
was introduced in 4.4BSD and is not available under Linux. The function__fpurge()
was introduced in Solaris, and is present in glibc 2.1.95 and later.
That said, the major usage-side difference is,
Calling fflush()
with input stream is undefined behavior.
If
stream
points to an output stream or an update stream in which the most recent operation was not input, thefflush
function causes any unwritten data for thatstream
to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.
Calling fpurge()
with input stream is defined.
For input streams this discards any input read from the underlying object but not yet obtained via
getc(3)
; this includes any text pushed back viaungetc(3)
.
Still, try to stick to fflush()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With