C++ Standard Library C++ STL Library

C++ <cstdio> - fflush() Function



The C++ <cstdio> fflush() function flushes the buffer of a stream.

If the given stream is open for writing (or if it is open for updating and the last i/o operation was an output operation) any unwritten data from the stream's buffer is written to the associated output device.

If stream is a null pointer, all open output streams are flushed.

In all other cases, the behavior is undefined.

Syntax

int fflush ( FILE * stream );

Parameters

stream Specify a pointer to a FILE object that specifies a buffered stream.

Return Value

On success, returns zero. On error, returns EOF and sets the error indicator ferror().

Example:

In the example below, fflush() is used to flush the buffer of stream.

#include <cstdio>
 
int main (){
  int x;
  char mybuffer[1024];

  setvbuf(stdout, mybuffer, _IOFBF, 1024);
  printf("Enter an integer - ");

  fflush(stdout);

  scanf("%d",&x);
  printf("You have entered %d.", x);

  return 0;
}

If the following input is entered:

10

The output of the above code will be:

Enter an integer - 10
You have entered 10.

❮ C++ <cstdio> Library

5