I am (re-)learning C-programming, so the following question is for the sake of understanding. Using scanf() I learned (or found out myself, it does not really take long to come to this point) that flushing stdin is a good thing. I further found out (with your help) that fflush(stdin) is not a standard thing to do and e.g. does not work with gcc. I moved on to use the code snippet below for flushing stdin. It was provided here (thanks). It works fine, if stdin is not empty. Yet, if stdin is empty it does not work. The fgetc() in my flushing function just waits and blocks the program. And in fact all other stdin-reading functions that I know of so far (scanf(), fgetc(), gets(), fgets(), getchar()) all show the same behaviour. So my flushing function is actually a conditional flushing: it only flushes if the buffer is not empty. If it is empty the flushing blocks the program and waits for an input to stdin. This is not what I want. So, what I need is a way to check if stdin is empty. If it is I continue. If it isn't I run my flushing function.
So, what I am looking for is a C standard way to check if stdin is empty.
Thanks a lot for any help!
void flush_line(FILE *);
/* function to flush stdin in a C standard way since*/
/* fflush(stdin) has undefined behaviour. */
/* s. http://stackoverflow.com/questions/20081062/fflushstdin-does-not-work-compiled-with-gcc-in-cygwin-but-does-compiled-with-v */
void flush_line(FILE *fin)
{
int ch;
do
{
ch = fgetc(fin);
} while (ch != '\n' && ch != EOF);
}