Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear stdin before getting new input?

Tags:

c

stdin

getchar

I have read about 5-10 different advices how to clear stdin, but none of them suits my needs. The thing is that fflush(stdin) worked perfectly at my computer, but unfortunately it doesn't seem to work everywhere, so I need something with the same functionality. Every other way I tried clears stdin when it is not empty but requires user input when stdin IS empty, which means it requires input in a moment I dont want to get any (+ it discards it anyway).

The question is: Can I somehow make sure, that stdin IS empty before I require user input? (and if not, THEN and only then clear it somehow?) something like:

if (stdin is NOT empty) 
    while (getchar() != '\n')
        continue;

EDIT: the thing is that I load characters from stdin one by one and at some point, a part of the input from previous iteration might or might not get discarded. either way, I need to have clear stdin before I ask the user for another input to be processed. Clearing the buffer itself is not such a big deal, the problem is what happens when the input is empty when the program gets to the point of clearing stdin, because in that moment the program needs another input which is going to be eaten by the clearing function. Thats what I want to get rid of. (when I could use fflush(stdin); I just knew, that for the next line of my program the stdin WILL be empty no matter what, no questions asked...)

like image 747
Tom Avatar asked Apr 19 '16 09:04

Tom


People also ask

How do you flush Stdin?

The function fflush(stdin) is used to flush or clear the output buffer of the stream. When it is used after the scanf(), it flushes the input buffer also. It returns zero if successful, otherwise returns EOF and feof error indicator is set.

Why do we need to flush the input buffer?

"Flushing the input buffer" refers to the attempt to discard unwanted characters from the input stream so that they do not perturb later input calls.

How do you clear a buffer in C++?

In order to clear the input buffer after the user has entered too many characters, you will need to clear the status flags of the input stream and then ignore all cahracters up to the newline. This can be done like so: cin. clear(); cin.

What is rewind Stdin?

rewind is a function in the C programming language that is specified in the stdio. h library header file. The function moves the file position indicator to the beginning of the specified stream, while also clearing the error and EOF flags associated with that stream.


2 Answers

How to clear stdin before getting new input?
.. so I need something with the same functionality.

With portable C this is not possible.


Instead suggest a different (and more usual C) paradigm:
Insure previous input functions consumes all the previous input.

fgets() (or *nix getline()) is the typical approach and solves most situations.

Or roll your own. The following reads an entire line, but does not save extra input.

int mygetline(char *buf, size_t size) {
  assert(size > 0 && size <= INT_MAX);
  size_t i = 0;
  int ch;
  while ((ch = fgetc(stdin)) != EOF) {  // Read until EOF ...
    if (i + 1 < size) {
      buf[i++] = ch;
    }
    if (ch == '\n') {  // ... or end of line
      break;  
    }
  } 
  buf[i] = '\0';
  if (i == 0) { 
    return EOF;
  }
  return i;
}
like image 157
chux - Reinstate Monica Avatar answered Sep 21 '22 03:09

chux - Reinstate Monica


From a similar question, Use poll() with fds.fd set to 0 (stdin), fds.events set to POLLIN, nfds set to 1, and timeout set to zero. After calling poll(), fds.revents will be set to zero if the buffer is empty, and to POLLIN otherwise.

struct pollfd fds = {0, POLLIN, 0};
poll(&fds, 1, 0);
if(fds.revents == POLLIN}
    printf("stdin buffer is not empty");

This solution will work on posix-compliant systems, but not Windows. Use select() for portability.

like image 28
Jason Fry Avatar answered Sep 20 '22 03:09

Jason Fry