home *** CD-ROM | disk | FTP | other *** search
- 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
- From: maupb@csv.warwick.ac.uk (Mr J L Saunders)
- Newsgroups: comp.unix.questions
- Subject: Re: Reading in single characters from stdin
- Message-ID: <1eo84hINN3tu@clover.csv.warwick.ac.uk>
- Date: 22 Nov 92 15:14:25 GMT
- References: <1en5ffINN1ue@cumin.csv.warwick.ac.uk>
- Organization: Computing Services, University of Warwick, UK
- Lines: 55
- NNTP-Posting-Host: clover.csv.warwick.ac.uk
-
- In article <1en5ffINN1ue@cumin.csv.warwick.ac.uk> cstaddc@csv.warwick.ac.uk (Mr M A Stuart) writes:
- %
- %
- % How can I read in characters from stdin and save the in a variable for
- %processing as single entities ?
- %
- %
- % Any suggestions ?
- %
- %
- %Mark. (cstaddc@uk.ac.warwick)
-
- If you want to do this from within C, I'd #include <curses.h>, and use the
- cusrses routines cbreak() and nocbreak(). Alternatively, you can #include
- <sgtty.h>, and then use the following routines. In fact, it's probably better
- to use these, unless you're going to use the rest of curses.
-
- What you'd do is call cbreakon(), then use getchar() to return a single
- character. So your code would be something like...
-
- inputroutine() {
- char inputchar;
-
- cbreakon();
- inputchar=getchar();
-
- whatever else you want to do...
-
- cbreakoff();
- }
-
- cbreakon()
- {
- struct sgttyb terminal;
-
- if ( ioctl (0, TIOCGETP, &terminal) ) return ( -1 );
- terminal.sg_flags |= CBREAK;
- if ( ioctl (0, TIOCSETP, &terminal) ) return ( -1 );
- return ( 0 );
- }
-
-
- cbreakoff()
- {
- struct sgttyb terminal;
-
- if ( ioctl (0, TIOCGETP, &terminal) ) return ( -1 );
- terminal.sg_flags &= ~CBREAK;
- if ( ioctl (0, TIOCSETP, &terminal) ) return ( -1 );
- return ( 0 );
- }
- --
- Jason L Saunders [ RouE ]
- email: maupb@csv.warwick.ac.uk
- snail: Warwick Business School, University of Warwick, Coventry CV4 7AL, UK
-