Tuesday, October 05, 2004

fflush(stdin)

from http://www.eskimo.com/~scs/C-faq/q12.26.html

How can I flush pending input so that a user's typeahead isn't read at the next prompt? Will fflush(stdin) work?


fflush is defined only for output streams. Since its definition of ``flush'' is to complete the writing of buffered characters (not to discard them), discarding unread input would not be an analogous meaning for fflush on input streams.

There is no standard way to discard unread characters from a stdio input stream, nor would such a way be sufficient unread characters can also accumulate in other, OS-level input buffers.

References: ANSI Sec. 4.9.5.2
ISO Sec. 7.9.5.2
H&S Sec. 15.2



1 comment:

Ahamed EN said...

To flush the input stream, do this

char ch;
while((ch = getc(stdin)) != EOF && ch != '\n');

Regards,
Ahamed