INF: rewind(stdin) Clears Keyboard Buffer

ID Number: Q41159

5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a

MS-DOS | OS/2

Summary:

To clear the keyboard buffer in Microsoft C versions 5.0, 5.1, 6.0,

6.0a, 6.0ax, and C/C++ version 7.0, use the function rewind() with the

stream STDIN, which is associated with the keyboard by default. The

function fflush() clears the buffers that C programs use for stream

level I/O. It does not clear the device buffer.

More Information:

The following is an example that shows what happens when the

keyboard buffer is not cleared, and then how to clear it:

Sample Code

-----------

/* Compile options needed: none

*/

#include <stdio.h>

void main( )

{

int ch ;

puts( "Input two or more chars. One is read now." );

ch = getchar();

putchar( ch );

putchar( '\n' );

puts( "The next char is taken from stdin." );

ch = getchar( );

putchar( ch );

putchar( '\n' );

rewind( stdin );

puts( "Input two or more chars. One is read now." );

ch = getchar();

putchar( ch );

putchar( '\n' );

rewind( stdin );

puts( "You must enter another char because of the rewind(stdin)" );

ch = getchar( );

putchar( ch );

putchar( '\n' );

}

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00