Mail Archives: djgpp/1995/02/27/10:04:06
From:
Message-Id: <9502270633 DOT AA23500 AT unlinfo DOT unl DOT edu>
Subject: Question reagarding kbhit()
To: djgpp AT edu DOT clarkson DOT soe DOT sun
Date: Mon, 27 Feb 1995 00:33:38 -0600 (CST)
X-Mailer: ELM [version 2.4 PL20]
Content-Type: text
Content-Length: 581
Sender: djgpp-bounces AT edu DOT clarkson DOT soe DOT sun
jkeene AT edu DOT unl DOT unlinfo (Jon Keene) , who signs himself Rumball,
asks:
>
>Could anyone tell me why it is that when I run the following
>program, it pauses for the keyboard hit _before_ doing any output?
>
Short answer: yes.
Longer: Because stdout is line-buffered by default and you haven't flushed
its buffer. The STDIO libraries save wear and tear on system calls
by buffering data in various ways. Streams are either unbuffered
(every call causes an immediate write()) line-buffered (write() only
gets called when a newline is output) or block-buffered (only do
a write() when N blocks of data have been output.) stdout is usually
line-buffered and stderr is unbuffered. You can force buffers to be
written when they wouldn't by using fflush(stream). The STDIO library
does this automatically so far as I recall in the special case where
you are about to read from stdin ; it flushes stdout. This is on the
assumption that they both usually point at your
terminal/screen/keyboard.
kbhit() has nothing to do with the stdio library and works at a lower,
bit-twiddlier level. Use fflush(stdout) before the while(!kbhit())
loop, or change stdout to be unbuffered. If you're doing lots of
putc(), the latter is a bad idea.
------------------------------------------------------------------------------
Kevin Ashley K DOT Ashley AT Ulcc DOT ac DOT uk
Systems Development Group Manager http://www.ulcc.ac.uk/staff/Kevin+Ashley
University of London Computer Centre. ...ukc!ncdlab!K.Ashley
Founder inductee in the WWW Useless Page Hall of Fame
This is not a signature
- Raw text -