fflush

Description

Flushes a stream.

#include <stdio.h>

int fflush( FILE *stream );

stream Pointer to FILE structure  

Remarks

If the file associated with stream is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer. The fflush function negates the effect of any prior call to ungetc against stream.

Buffers are automatically flushed when they are full, when the stream is closed, or when a program terminates normally without closing the stream. Also, fflush(NULL) flushes all streams opened for output.

The stream remains open after the call. The fflush function has no effect on an unbuffered stream.

Return Value

The fflush function returns the value 0 if the buffer was successfully flushed. The value 0 is also returned in cases in which the specified stream has no buffer or is open for reading only. A return value of EOF indicates an error.

Note:

If fflush returns EOF, data may have been lost because of a failed write. When setting up a critical error handler, it is safest to turn buffering off with the setvbuf function or to use low-level I/O routines such as _open, _close, and _write instead of the stream I/O functions.

Compatibility

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

See Also

fclose, _flushall, setbuf

Example

/* FFLUSH.C */

#include <stdio.h>

#include <conio.h>

void main( void )

{

int integer;

char string[81];

/* Read each word as a string. */

printf( "Enter a sentence of four words with scanf: " );

for( integer = 0; integer < 4; integer++ )

{

scanf( "%s", string );

printf( "%s\n", string );

}

/* You must flush the input buffer before using gets. */

fflush( stdin );

printf( "Enter the same sentence with gets: " );

gets( string );

printf( "%s\n", string );

}

Output

Enter a sentence of four words with scanf: This is a test

This

is

a

test

Enter the same sentence with gets: This is a test

This is a test