How to check if stdin is empty in C

此生再无相见时 提交于 2020-01-02 06:13:30

问题


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

}

回答1:


You can use select or poll to check whether a file descriptor is readable. But is not in ANSI C.




回答2:


I solved that in cancerous way, but works fine. If only works when the stdin is dirty (not-empty), so then just send a \n to stdin always before clear it. Yes, I know, seems a very ugly solution, but is the only sane way I can think to be portable, since select or poll doesn't works with Windows).

Here is:

void flush_stdin() {
    char c;
    ungetc('\n', stdin); // ensure that stdin is always dirty
    while(((c = getchar()) != '\n') && (c != EOF));
}



回答3:


Don't think of stdin as a place where data is or is not available. It's called a stream for a reason: you're always interested in the next data from it, whenever that might be available.

Concretely, you decide to "flush" (maybe "drain" would be a better word) the stream for a reason. Perhaps you requested an integer from the user and they entered letters instead. You know, then, that the stream isn't "empty" because there are letters in it! So reading until the next newline is a reasonable idea.

Or perhaps (for compatibility with some other utility?) you have a protocol where a certain line of input is irrelevant. Then you can "drain" that line; it doesn't matter whether the user has typed anything yet because you have to wait until they type it before you can accept any more input from them anyway.



来源:https://stackoverflow.com/questions/20299348/how-to-check-if-stdin-is-empty-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!