I have a question concerning stdin buffer content inspection.
This acclaimed line of code:
int c; while((c = getchar()) != '\n' && c != EOF);
deals efficiently with discarding stdin-buffer garbage, in case there is a garbage found. In case the buffer is empty, the program execution wouldn't go past it.
Is there a way of checking if there is garbage in the stdin-buffer at all (no matter if it's there by user error, typeahead or whichever reason), and executing the "fflush-replacement line" from above only in case there is a garbage found?
I'd prefer to keep it programmatically all in plain-UNIX-flavor-of standard C, without having to use special parsing tools, no yacc, bison, python, ruby, shell scripts etc., no Windows API, please.
Thanks in advance!
UPDATE:
I hope this example tells a bit more of my question:
//...
//this line should make sure stdin buffer is free from accidentally typed content
int c; while (( c = getchar()) != '\n' && c != EOF);
//this line won't show in case buffer is already clean
printf("Please enter an arbitrary number of float or symbolic values:\n");
//this line should read the real input user is being asked for
char* p = fgets(text, TEXT_SIZE, stdin);
if(p != NULL)
parse_and_process(text);
//...
The problem happens when there is no accidental input. The "garbage" is here considered anything that may stay in the buffer at the moment printf( ) prompt would appear. Is there a way of getting around the first line in case the buffer is already clean?