Open main menu

Wikibooks β

C++ Programming/Code/Standard C Library/Functions/fflush

< C++ Programming‎ | Code/Standard C Library‎ | Functions


#include <cstdio>
int fflush( FILE *stream );

If the given file stream is an output stream, then fflush() causes the output buffer to be written to the file. If the given stream is of the input type, the behavior of fflush() depends on the library being used (for example, some libraries ignore the operation, others report an error, and others clear pending input).

fflush() is useful when either debugging (for example, if a program segfaults before the buffer is sent to the screen), or it can be used to ensure a partial display of output before a long processing period.

By default, most implementations have stdout transmit the buffer at the end of each line, while stderr is flushed whenever there is output. This behavior changes if there is a redirection or pipe, where calling fflush(stdout) can help maintain the flow of output.

printf( "Before first call\n" );
fflush( stdout );
printf( "Before second call\n" );
fflush( stdout );
Related topics
fclose - fopen - fread - fwrite - getc - putc