Difference between int fpurge() and int fflush() in C
Asked Answered
I

2

6

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.

Industrials answered 31/5, 2016 at 5:31 Comment(2)
Judging from the Linux manual page for fpurge(), one problem with fpurge() is that it is non-standard, being derived from BSD 4.4, so it is not available everywhere. The Mac OS X (BSD) man page describes fpurge() but doesn't mention its provenance — though it does say that fseek(), list on the same page, conforms to Standard C so (by inference) fpurge() doesn't.Pickaninny
See also How can I flush data from a tty input queue on a Unix system?Pickaninny
H
4

"... 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 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).

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).

Haler answered 31/5, 2016 at 6:7 Comment(0)
G
2

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.

  • For fflush(), the data is forced to be written to disk.
  • For 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, the fflush function causes any unwritten data for that stream 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 via ungetc(3).

Still, try to stick to fflush().

Garnish answered 31/5, 2016 at 5:33 Comment(4)
fflush on input streams may be left undefined by ISO C, but it is specified by POSIX. The effect is similar to that quoted for fpurge except that it only works on seekable input streams — i.e. it can be used to ensure that stale data wasn't buffered from a disk file, but not to discard data buffered from a pipe or tty.Saraisaraiya
@Saraisaraiya Right, but still, it's implementation defined.Garnish
^^ I mean to say, the applicability of fflush(stdin). IIRC, MSVC defines that, but a code with that line is certainly not easily portable. :)Garnish
"Calling fflush() with input stream is undefined behavior." and "Calling fpurge() with input stream is defined." is not consistent. Per the C standard fflush(file_in_input_mode) is UB and fpurge() is also not defined behavior given it is not in the standard. Both of these are defined within Linux and other platforms.Expectant

© 2022 - 2024 — McMap. All rights reserved.