Re: WOT: Testing stdin

New Message Reply Date view Thread view Subject view Author view

Thom DeCarlo (trdecarlo++at++tasc.com)
Wed, 23 Jun 1999 23:13:35 -0400


Hi,

Don Hatch wrote:

> 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.

Well, I guess I have to admit it. I'm not writing this on an SGI. I'm on WinNT
using VC++ 6.0 and VegaNT.

I can't find anything on this machine that defines the select() function. And
the read() function still blocks until '\n' is entered. I did find an
_filelength() function which I thought might work, but it just returns an
error whether anything is on the input buffer or not.

Thom


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2.0b2 on Wed Jun 23 1999 - 20:06:58 PDT

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