check whether fgets would block

Posted by lv on Stack Overflow See other posts from Stack Overflow or by lv
Published on 2010-04-29T09:07:32Z Indexed on 2010/04/29 9:17 UTC
Read the original article Hit count: 214

Filed under:
|
|
|
|

Hi, I was just wondering whether in C is it possible to peek in the input buffer or perform similar trickery to know whether a call to fgets would block at a later time. Java allows to do something like that by calling BufferedReader.ready(), this way I can implement console input something like this:

while (on && in.ready()) { 
  line = in.readLine();
  /* do something with line */
  if (!in.ready())
    Thread.sleep(100);
}

this allows an external thread to gracefully shutdown the input loop by setting on to false; I'd like to perform a similar implementation in C without resorting to non portable tricks, I already know I can make a "timed out fgets" under unix by resorting to signals or (better, even though requering to take care of buffering) reimplement it on top of recv/select, but I'd prefer something that would work on windows too.

TIA

© Stack Overflow or respective owner

Related posts about fgets

Related posts about nonblocking