Re: WOT: Testing stdin

New Message Reply Date view Thread view Subject view Author view

Don Hatch (hatch++at++hell.engr.sgi.com)
Wed, 23 Jun 1999 18:36:24 -0700


On Jun 23, 9:21pm, Thom DeCarlo wrote:
> Subject: WOT: Testing stdin
> Hi,
> I apologize for this message being Way Off Topic, but I'm at a loss for
> how to continue.
>
> The problem is, how do I test whether anything has been typed to stdin
> without blocking the program if the input buffer is empty? Of course,
> the simple things don't work. I've tried getc(), fgetc(), gets(),
> scanf(), .... They all block until a '\n' is typed. I just want the test
> to fail and continue if nothing is waiting in the buffer.
>
> Any ideas from you C++ gurus out there?
>
> TIA,
> Thom
>
>
> -----------------------------------------------------------------------
> List Archives, FAQ, FTP: http://www.sgi.com/software/performer/
> Submissions: info-performer++at++sgi.com
> Admin. requests: info-performer-request++at++sgi.com
>-- End of excerpt from Thom DeCarlo

Not off topic at all! You need to know how to do this
to get keyboard input in any Performer simulation loop.

Use select() to query whether stdin (fileno = 0) has characters
waiting to be read.
If it says yes, then you should be able to call getc()
or fgets() to get a line of input without it blocking.

You'll have to experiment with the details of stdio buffering
(e.g. what happens when there's more than one line of input waiting?
I suspect the second line might get buffered and so you'll get fooled
into thinking it's not there).
You can probably avoid this issue by calling setbuf(stdin, NULL),
or use read() directly instead of the buffered stdio calls.

Don

-- 
Don Hatch  hatch++at++sgi.com  (650) 933-5150  Silicon Graphics, Inc.

New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2.0b2 on Wed Jun 23 1999 - 18:36:54 PDT

This message has been cleansed for anti-spam protection. Replace '++at++' in any mail addresses with the '@' symbol.