From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: Simple Getch problem Date: Sat, 23 Nov 1996 23:33:13 -0800 Organization: Three pounds of chaos and a pinch of salt Lines: 63 Message-ID: <3297FA39.15DD@cs.com> References: <5783b1$gbk AT yama DOT mcc DOT ac DOT uk> Reply-To: fighteer AT cs DOT com NNTP-Posting-Host: ppp102.cs.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: mfepsdp AT afs DOT mcc DOT ac DOT uk To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp 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 package, which uses BIOS calls to interface directly with the text screen and keyboard; thus they completely bypass the functions. It is in general a bad idea to mix functions from and 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 | ---------------------------------------------------------------------