home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / question / 13763 < prev    next >
Encoding:
Internet Message Format  |  1992-11-22  |  1.9 KB

  1. Path: sparky!uunet!ornl!rsg1.er.usgs.gov!darwin.sura.net!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!agate!doc.ic.ac.uk!uknet!warwick!warwick!not-for-mail
  2. From: maupb@csv.warwick.ac.uk (Mr J L Saunders)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: Reading in single characters from stdin
  5. Message-ID: <1eo84hINN3tu@clover.csv.warwick.ac.uk>
  6. Date: 22 Nov 92 15:14:25 GMT
  7. References: <1en5ffINN1ue@cumin.csv.warwick.ac.uk>
  8. Organization: Computing Services, University of Warwick, UK
  9. Lines: 55
  10. NNTP-Posting-Host: clover.csv.warwick.ac.uk
  11.  
  12. In article <1en5ffINN1ue@cumin.csv.warwick.ac.uk> cstaddc@csv.warwick.ac.uk (Mr M A Stuart) writes:
  13. %
  14. %
  15. %    How can I read in characters from stdin and save the in a variable for
  16. %processing as single entities ?
  17. %
  18. %
  19. %    Any suggestions ?
  20. %
  21. %
  22. %Mark. (cstaddc@uk.ac.warwick)
  23.  
  24. If you want to do this from within C, I'd #include <curses.h>, and use the
  25. cusrses routines cbreak() and nocbreak(). Alternatively, you can #include
  26. <sgtty.h>, and then use the following routines. In fact, it's probably better
  27. to use these, unless you're going to use the rest of curses.
  28.  
  29. What you'd do is call cbreakon(), then use getchar() to return a single
  30. character. So your code would be something like...
  31.  
  32. inputroutine() {
  33. char inputchar;
  34.  
  35.   cbreakon();
  36.   inputchar=getchar();
  37.  
  38.   whatever else you want to do...
  39.  
  40.   cbreakoff();
  41. }
  42.  
  43. cbreakon()
  44. {
  45.       struct sgttyb terminal;
  46.  
  47.       if ( ioctl (0, TIOCGETP, &terminal) ) return ( -1 );
  48.       terminal.sg_flags |= CBREAK;
  49.       if ( ioctl (0, TIOCSETP, &terminal) ) return ( -1 );
  50.     return ( 0 );
  51. }
  52.  
  53.  
  54. cbreakoff()
  55. {
  56.     struct sgttyb terminal;
  57.  
  58.       if ( ioctl (0, TIOCGETP, &terminal) ) return ( -1 );
  59.       terminal.sg_flags &= ~CBREAK;
  60.       if ( ioctl (0, TIOCSETP, &terminal) ) return ( -1 );
  61.     return ( 0 );
  62. }
  63. -- 
  64.                        Jason L Saunders [ RouE ]      
  65.                      email: maupb@csv.warwick.ac.uk
  66. snail: Warwick Business School, University of Warwick, Coventry CV4 7AL, UK
  67.