Reputation: 65
I'd like to know how I can do to clear the standard input buffer, regardless of the operating system I'm using.
I know that in Windows I can use fflush
and Linux fpurge
, but I would like a single solution that works for both (does not necessarily have to be a function).
Upvotes: 0
Views: 202
Reputation: 140455
Depending on what you mean by "clear the standard input buffer", this may do:
int c;
do c = getchar(); while (c != EOF && c != '\n');
This absorbs up to EOF or a newline, whichever comes first.
The main situation where this wouldn't be what you want is if you don't want it to maybe block until the user presses Enter. In that case, you're SOL; there is no universal mechanism.
Upvotes: 1