home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:19425 comp.unix.questions:15324
- Path: sparky!uunet!mcsun!uknet!acorn!uniplex!cpm
- From: cpm@uniplex.co.uk (Chris Murray)
- Newsgroups: comp.lang.c,comp.unix.questions
- Subject: Re: Non-Blocking I/O
- Message-ID: <1665@uniplex.co.uk>
- Date: 8 Jan 93 20:37:00 GMT
- References: <1993Jan6.184330.4041@risky.ecs.umass.edu>
- Reply-To: cpm@uniplex.UUCP (Chris Murray)
- Organization: Uniplex Limited, Redwood House, Hemel Hempstead
- Lines: 38
-
- In article <1993Jan6.184330.4041@risky.ecs.umass.edu> chou@umvlsi.ecs.umass.edu writes:
- >
- >
- >I am trying to use non-blocking I/O in C and am having difficulties.
- >I would like to have a read routine that would return immediately if
- >there is no input available in standard input. This is what I have
- >tried:
- >
- >#include <stdio.h>
- >#include <fcntl.h>
- >
- >main ()
- >{
- > char buf[1];
- > size_t size = 1;
- >
- > /* Set stdin to be non-blocking. */
- > fcntl (stdin, F_SETFL, O_NDELAY);
- >
- > /* Prompt for some input. */
- > printf ("Input a character: ");
- >
- > /* Get a character from stdin. If there is nothing in stdin, I want
- > this to return immediately. */
- > fread (buf, size, 1, stdin);
- >}
- >
- >Any help with this would be appreciated.
- >
- >Please send your mail to chou@ecs.umass.edu
- >
- >Thanks,
- >Yon
-
- You're getting streams confused with file descriptors. Look at the man
- pages for fcntl and you'll see it takes a file descriptor, not a stream.
- Change the fcntl line to fcntl (fileno(stdin), F_SETFL, O_NDELAY);
- and it should work.
-