Recover stdin from eof in C

I use the C code below to read data input from a terminal. If the user enters EOF, for example. by pressing ^ C, stdin closes and subsequent attempts to read from it, for example. via getchar () or scanf (), will throw an exception.

Is there anything I can do in C to β€œrestore” my program, in the sense that if some user accidentally enters EOF, it will be ignored, so I can read from stdin again?

#include <stdio.h>

int main(void)
{
    int res_getchar=getchar();
    getchar();
    return 0;
}
+4
source share
3 answers

Using ungetc()to return a character, you can clear the EOF indicator for a stream.

C99 Β§7.19.7.11 Function ungetc

 int ungetc(int c, FILE *stream);

ungetc . , , .

+1

- stdin, ^D, - : ( ), , ( ), .

+4

, . EOF, stdin.

, , stdin , .. , , stdin - .

0
source

All Articles