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...)
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.
"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.
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.
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.
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With