Mail Archives: djgpp/1996/11/24/07:43:19
Mr David Potts wrote:
>
> I installed djgpp v2.01. I then found the following odd
> behavior. I compiled the following program:
[snip]
> But when I ran it, it waited for a key press *then* printed
> the text rather than the other way round. What is going on?
DJGPP uses buffered output for stdin/stdout, which means that unless you
explicitly flush the output buffer, it doesn't print anything on the
screen until you a) print a newline, b) call any stdin-based input
function. This is where your program fails, because you use getch()
instead of getc() for your input. getch() is part of the <conio.h>
package, which uses BIOS calls to interface directly with the text
screen and keyboard; thus they completely bypass the <stdio.h>
functions. It is in general a bad idea to mix functions from <stdio.h>
and <conio.h> unless you are very careful to flush all the stdin/stdout
buffers first.
To fix your code, do one of these things:
/* use the newline to flush stdout */
printf( "Before getch\n" );
getch( );
/* explicitly flush stdout */
printf( "Before getch" );
fflush( stdout );
getch( );
/* use all stdio functions */
printf( "Before getc" );
getc( );
/* use all conio functions */
cprintf( "Before getch" );
getch( );
/* (dangerous) instead of stdout, use stderr which is not buffered */
fprintf( stderr, "Before getch" );
getch( );
Here's a brief list of the differences between stdio and conio
functions:
- stdio uses DOS i/o calls; conio writes to text memory and reads from
BIOS.
- stdio is line buffered by default; conio is not and cannot be
buffered.
- stdio can be redirected with DOS redirection calls; conio CANNOT be
redirected. (*)
- stdio uses a DOS file buffer; conio uses the BIOS keyboard buffer.
(*) Actually, you can, but only by writing interrupt handlers to hook
the BIOS keyboard interrupt. I know of no way at all to "redirect"
output short of constantly scanning text video memory.
--
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I | fighteer AT cs DOT com |
| Proud owner of what might one | http://www.cs.com/fighteer |
| day be a spectacular MUD... | Plan: To make Bill Gates suffer |
---------------------------------------------------------------------
- Raw text -