home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19425 < prev    next >
Encoding:
Internet Message Format  |  1993-01-08  |  1.5 KB

  1. Xref: sparky comp.lang.c:19425 comp.unix.questions:15324
  2. Path: sparky!uunet!mcsun!uknet!acorn!uniplex!cpm
  3. From: cpm@uniplex.co.uk (Chris Murray)
  4. Newsgroups: comp.lang.c,comp.unix.questions
  5. Subject: Re: Non-Blocking I/O
  6. Message-ID: <1665@uniplex.co.uk>
  7. Date: 8 Jan 93 20:37:00 GMT
  8. References: <1993Jan6.184330.4041@risky.ecs.umass.edu>
  9. Reply-To: cpm@uniplex.UUCP (Chris Murray)
  10. Organization: Uniplex Limited, Redwood House, Hemel Hempstead
  11. Lines: 38
  12.  
  13. In article <1993Jan6.184330.4041@risky.ecs.umass.edu> chou@umvlsi.ecs.umass.edu writes:
  14. >
  15. >
  16. >I am trying to use non-blocking I/O in C and am having difficulties.
  17. >I would like to have a read routine that would return immediately if
  18. >there is no input available in standard input.  This is what I have
  19. >tried:
  20. >
  21. >#include <stdio.h>
  22. >#include <fcntl.h>
  23. >
  24. >main ()
  25. >{
  26. >    char buf[1];
  27. >    size_t size = 1;
  28. >
  29. >    /*  Set stdin to be non-blocking.  */
  30. >    fcntl (stdin, F_SETFL, O_NDELAY);
  31. >
  32. >    /*  Prompt for some input.  */
  33. >    printf ("Input a character:  ");
  34. >
  35. >    /*  Get a character from stdin.  If there is nothing in stdin, I want
  36. >    this to return immediately.  */
  37. >    fread (buf, size, 1, stdin);
  38. >}
  39. >
  40. >Any help with this would be appreciated.
  41. >
  42. >Please send your mail to chou@ecs.umass.edu
  43. >
  44. >Thanks,
  45. >Yon
  46.  
  47. You're getting streams confused with file descriptors.  Look at the man
  48. pages for fcntl and you'll see it takes a file descriptor, not a stream.
  49. Change the fcntl line to fcntl (fileno(stdin), F_SETFL, O_NDELAY);
  50. and it should work.
  51.