I've run a few tests on this and it appears to flush both input and output streams, but I've only tried it on Windows systems. Could someone test this out on other systems (ie: Linux, Mac, etc) to see how well it fares in portability? If it does, I'll ask the admins to add this to the FAQ. Thanks.
Code:
#include <stdio.h>
int flush( FILE* stream )
{
return fseek( stream, 0, SEEK_END ) == 0;
}
int get( void )
{
flush( stdin );
return fgetc( stdin );
}
int main( int argc, char** argv )
{
int
ch;
FILE*
out = 0;
if( argc == 1 )
fprintf( stderr, "Stream Flush Test Usage: %s [output_files]\n", *argv );
else
{
while( *( ++argv ) )
{
out = fopen( *argv, "a" );
if( out == 0 )
fprintf( stderr, "Error: could not open output file '%s'\n", *argv );
else
{
printf( "Enter a string of characters > " );
get( );
printf( "Enter a character not in the set above > " );
ch = get( );
printf( "Sending character '%c' to output stream\n", ch );
fputc( ch, out );
printf( "Press [ENTER] to flush the output stream > " );
get( );
flush( out );
fclose( out );
}
}
printf( "Press [ENTER] after verifying output >" );
get( );
}
return 0;
}