Re: fflush(stdin)
- From: Stephane CHAZELAS <this.address@xxxxxxxxxx>
- Date: Wed, 09 May 2007 13:03:57 GMT
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
.
- Follow-Ups:
- Re: fflush(stdin)
- From: Geoff Clare
- Re: fflush(stdin)
- From: Thomas Dickey
- Re: fflush(stdin)
- References:
- fflush(stdin)
- From: John Doe
- fflush(stdin)
- Prev by Date: Re: TCP-IP timeout on Unixware or Solaris
- Next by Date: Seg Fault before execute main()
- Previous by thread: Re: fflush(stdin)
- Next by thread: Re: fflush(stdin)
- Index(es):
Relevant Pages
|