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) {

fd = dup(STDIN_FILENO); /* save stdin */
dup2(fd, STDIN_FILENO); /* restore */
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.


Relevant Pages

  • Re: Cannot return values of char variable
    ... - buffer = ... Since you seem to be trying to return a char pointer ... int id = random; ... content is interpreted as a string. ...
  • [KGDB PATCH][2/7] Serial updates, take 2
    ... Also make put_packet look at the char it reads, ... * Empty the receive buffer first, then look at the interface hardware. ... * This is the receiver interrupt routine for the GDB stub. ... -extern volatile int kgdb_connected; ...
  • Re: Write to file
    ... fwrite, outfile); ... This will repeatedly try to print the first character in buffer as ... int main(int argc, char *argv) ...
  • Re: Write to file
    ... int main(int argc, char *argv) ... I intended you you use c here not read another character! ... You reuse the buffer. ...
  • Re: newbie question: whats wrong with my code?
    ... why not declare buffer as a char * or unsigned char *? ... int clean_up; ... unsigned char buf[BUFSIZ]; ...