Look into the input buffer and discard extra characters in C

If I want to get a single input character in C, how can I check if additional characters have been sent, and if so, how can I clear that?

Is there a function that acts like getc (stdin) but that doesn't force the user to type in a character, so I can just put while(getc(stdin)!=EOF);

? Or a function to look into the next character in the buffer, and if it doesn't return NULL (or whatever), I could call the (nother) function that flushes stdin?

Edit

So right now scanf seems to be doing the trick, but is there a way to make it read the whole line , right down to the newline? Instead of the nearest space? I know I can just put "% s% s% s" or something in the format string, but can I handle an arbitrary number of spaces?

+1


a source to share


6 answers


You cannot clear the input stream. If you do this, you will be referencing undefined behavior. Best to do:

int main() {
  int c = getchar();
  while (getchar() != EOF);
  return 0;
}

      



Use magic scanf

:

#include <stdio.h>
#include <stdlib.h> 

#define str(s) #s
#define xstr(s) str(s)
#define BUFSZ 256

int main() {
  char buf[ BUFSZ + 1 ];
  int rc = scanf("%" xstr(BUFSZ) "[^\n]%*[^\n]", buf);
  if (!feof(stdin)) {
    getchar();
  }
  while (rc == 1) {
    printf("Your string is: %s\n", array);
    fflush(stdout);
    rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
    if (!feof(stdin)) {
        getchar();
    }
   }
   return 0;
}

      

+4


a source


You can use getline

to read a whole line of input.



Alternatively (in response to your original question), you can call select

or poll

on stdin to see if there are additional characters to read.

+1


a source


I had a similar problem today and found a way that seems to work. I don't know the details of your situation, so I don't know if this will work for you or not.

I am writing a routine that needs to get one character from the keyboard, and it must be one of three specific keystrokes ("1", "2" or "3"). If it is not one of them, the program should send an error message and send an error message for another attempt.

The problem is that, in addition to the character I enter returned by getchar (), the 'Enter' key press (which sends the keypress to the program) is stored in the input buffer. This (non-printable) newline is then returned by getchar () in an error-correcting loop, resulting in a second error message (since the newline is neither "1" nor "2", not '3'.)

The problem is even more complicated because I sometimes get ahead of myself and instead of typing one character, I will enter the name of a file that will ask for one of these parameters. I then have a whole string of unwanted characters in the buffer, causing a long list of error messages to scroll across the screen.

Not cool.

However, this seems to be fixed:

c = getchar();  // get first char in line
while(getchar() != '\n') ; // discard rest of buffer

      

The first line is the one that actually uses the entered character. The second line contains the remainder of the input buffer. It just creates a loop that pulls a character at a time from the input buffer. It does not specify the action to be performed during the loop. It just reads the character and if it is not a newline, it goes back to the next one. When it finds a new line, the loop ends and it moves on to the next order of business in the program.

+1


a source


We can make a function to clear the keyboard buffer, for example:

#include <stdio.h>

void clear_buffer(){
   char b;
   //this loop take character by character in the keyboard buffer using 
   //getchar() function, it stop when the variable "b" was
   //enter key or EOF.
   while (((b = getchar()) != '\n') && (b != EOF));
}

int main()
{
    char input;
    //get the input. supposed to be one char!
    scanf("%c", &input); 
    //call the clearing function that clear the buffer of the keyboard 
    clear_buffer(); 
    printf("%c\n",input); //print out the first character input
    // to make sure that our function work fine, we have to get the
    // input into the "input" char variable one more time
    scanf("%c", &input); 
    clear_buffer();  
    printf("%c\n",input);
    return 0;
}

      

+1


a source


Use a read that takes many characters (more than 1, maybe 256) and see how many are actually returned. You know, if you get more than one, if you only get one, they were all available.

You didn't mention the platform, and it's quite complicated. For example, on Unix (Linux), the normal mechanism will return a string of data - perhaps the one you used and a newline. Or maybe you convince your user to type ^ D (the default) to send the previous character. Or, maybe you are using a control function to place the terminal in raw mode (for example, programs such as vi

and emacs

do).

On Windows, I'm not sure: I think there is a function getch()

that does what you need.

0


a source


Why don't you use scanf instead of getc, using scanf u can get the whole string.

0


a source







All Articles