Re: fflush(stdin)



2007-05-08, 21:56(+02), John Doe:
Does it do anything? I think it shouldn't. But anyway, I'd like to flush
away all input characters waiting for getch() out of stdin.

You may want to try things like:

#include <stdio.h>
#include <unistd.h>
int main() {
char c;
int fd;

while ((c = getc(stdin)) != EOF) {
putchar(c);

fd = dup(STDIN_FILENO); /* save stdin */
fclose(stdin);
dup2(fd, STDIN_FILENO); /* restore */
close(fd);
stdin = fdopen(STDIN_FILENO, "r"); /* new stdin with new
buffer, the original buffer is lost of course */

}
return 0;
}

(error checking code is left to the reader).

If the file opened on stdin is a regular file, you can also play
with ftell and fseek to restore the cursor position after you've
reopened fd 0.

--
Stéphane
.