Streams and buffering problem
I have this problem in my code where I scanf a %d value, and if it is not converted I want to flush stdin and return to the main menu where a choice is presented.
Problem is that if the error condition occurs (say I enter "eep"), I return to the main menu with those characters on the stream and so scanf picks them up, and since 'e' is one of the menu options the case statement is triggered.
I thought that fflush(stdin) would do the trick, but apparently on GNU libc this only causes the contents of the buffer to be written to the stream.
Now I don't know much about buffered I/O, but i'm pretty sure what I want to do is also clear the stream somehow so subsequent calls to scanf don't pick up stray chars.
Hope someone can help with this.
The C standard for fflush() is to flush OUTPUT streams only.
The result is undefined for input streams. i.e., it may or may not do anything.
I found that theres a function __fpurge(FILE *stream) (from stdio_ext.h) that does exactly what I want, ie purges the buffer and the stream like a good enema.
However I think (but am not sure) thats only with GNU libc - i'd like something a bit more portable if possible.
Anyone know about that?
|All times are GMT -5. The time now is 07:37 AM.|