fflush
int fflush (FILE * stream); | stdio.h |
cplusplus.com |
Flush a stream.
If the given stream has been opened for writing operations the output buffer
is phisically written to the file.
If the stream was open for reading operations the content of the input
buffer is cleared.
The stream remains open after this call.
When a file is closed all the buffers associated with it are
automatically flushed.
If the program terminates, every
buffer is automatically flushed.
Parameters.
Return Value.
A 0 value indicates success.
If any errors occur, EOF is returned.
Example.
To demonstrate the use of fflush, we ask twice the user to input some words
in a sentence. Each time the program reads the first word with scanf and
flushes the rest. The next time the user is prompt the buffer will be
cleared so we will be able to obtain the first word of the new sentence.
/* fflush example */
#include <stdio.h>
main()
{
int n;
char string[80];
for ( n=0 ; n<2 ; n++ )
{
printf( "Enter some words: " );
scanf( "%s", string );
printf( "The first word you entered is : %s\n", string );
fflush ( stdin );
}
return 0;
}
Output.
Enter some words: Testing this program
The first word you entered is : Testing
Enter some words: It seems to work...
The first word you entered is : It
See also.
fclose,
fopen,
fread,
fwrite