问题
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?
回答1:
This acclaimed line of code:
int c; while((c = getchar()) != '\n' && c != EOF);
deals efficiently with discarding stdin-buffer garbage
No. This is just reading character until newline character is found or EOF is reached. I don't get what garbage you are talking about.
So by doing this you are consuming the newline character that is left in the stdin buffer. This is one of the ways of flushing the buffer.
回答2:
Edit to respond to OP comment.
You need to specify what is garbage. Only then can you (or anyone else) determine what input is valid.
Once you have determined that, you can write a routine that quietly consumes user input with the category of garbage until some non-garbage input is detected.
The conio.h header used in C programming on Windows (not Unix) contains functions for console input/output. Some of the most commonly used functions of conio.h are clrscr
, getch
, getche
, kbhit
etc.
Following is an example from here that illustrates how you might run a loop that consumes input until non-garbage appears over the keyboard... (in this case, it is written to work with escape key, using an adaptation of conio functions, but you can adapt it for your purposes)
do
{
ch = _getch();
ch = toupper( ch );
if (ch != 27) {
_cputs( "CHARACTER: " );
_putch( ch );
_putch( '\r' ); // Carriage return
_putch( '\n' ); // Line feed
}
} while( ch != 27 );
Original post
This does not answer Is there a way of checking if there is garbage in the stdin-buffer, but it addresses how to make sure it is clear...
Add one additional line to make it clear to the user how to begin, and because of your original code, it will result in clearing the buffer of any garbage, or unintended input before user is directed to enter additional values...:...
...
printf("hit <return> to begin");
while ( (c = getchar()) != '\n' && c != EOF );
printf("Please enter an arbitrary number of float or symbolic values:\n");
...
来源:https://stackoverflow.com/questions/28744759/plain-c-stdin-buffer-garbage-and-newline-eaters