I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingCStdio.h

fpurge

Flush a stream
+ View other 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 NULL, fflush flushes all open output streams.

The function fpurge erases any input or output buffered in the given \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 reference:getc. This includes any text pushed back via reference:ungetc.

Example:

Example - Flush a stream
Workings
#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, EOF is returned and the global variable errno 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.