flush a stream

You're viewing an older version of this page (#3456). View the current version.

View versions (3)

INTERFACE

#include <stdio.h> int fflush ( FILE *stream ) int fpurge ( FILE *stream )

DESCRIPTION

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 <span class="Dv">NULL</span>, fflush flushes <span class="Em">all</span> open output streams.

The function fpurge erases any input or output buffered in the given &#92;c 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.

Example 1

#include <stdio.h>

int main()
{
  char buffer[100];

  // open file for both reading and writing
  FILE *f = fopen("rw.txt", "rt+");
  if (f)
  {
    fputs("something", f);
    // flush the stream so we can use fgets
    fflush(f);
    fgets(buffer, 9, f);
    fclose(f);
  }
  return 0;
}

RETURN VALUES

Upon successful completion 0 is returned. Otherwise, <span class="Dv">EOF</span> is returned and the global variable <span class="Va">errno</span> is set to indicate the error.

ERRORS

EBADF The stream argument is not an open stream, or, in the case of flush, not a stream open for writing.

The function fflush may also fail and set errno for any of the errors specified for the routine write.

SEE ALSO

fopen