0

I want to write a function that would tell me if stdin buffer is empty. So, here's the code:

int buff_empty()
{
    char buffer[3];
    fgets(buffer, 3, stdin);
    if(strlen(buffer) > 1) return 0;
    return 1;
}

but the problem is - I don't want fgets() to wait for my input. I'm using my own function to read, for example, an integer, and then check with this func is buffer empty (was more than one number put) or not.

Function works well, but when I read a number (read function reads just to white space, just like scanf()), it waits for one breakline(Enter?) more. Is there any way to solve it?

Thanks in advance!

Samik
  • 37
  • 1
  • 2
  • 11
  • So, is there any way to put a breakline to stdin via code? That would work aswell, as fgets() waits for my input. – Samik Jan 10 '14 at 12:22

1 Answers1

0

Part of the problem is that you are kind of asking the program to read your mind. When it reads (or tries to read) from the input, and there's nothing there, how does it tell the difference between there being no more input and the input just hasn't been entered yet?

The normal solution is to put the input into a separate file, and look for the end-of-file to tell when there is no more input. So when the program tries to read the next number, it hits the end of file and knows that there isn't any more input.

Try reading up on the feof() function, or the possible return values of fgets().

Chris J. Kiick
  • 1,487
  • 11
  • 14