delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/27/03:15:20

From: George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: printf/getch problem
Date: 27 Jan 1998 07:41:03 GMT
Organization: Oxford University, England
Lines: 57
Message-ID: <6ak32f$1rc$2@news.ox.ac.uk>
References: <34CC5211 DOT 57270346 AT alcyone DOT com> <19980127034901 DOT WAA22568 AT ladder02 DOT news DOT aol DOT com>
NNTP-Posting-Host: sable.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On 27 Jan 1998 03:49:35 GMT in comp.os.msdos.djgpp Myknees
<myknees AT aol DOT com> wrote:

: In article <34CC5211 DOT 57270346 AT alcyone DOT com>, Erik Max Francis <max AT alcyone DOT com>
: writes:
: >Better yet, do not mix stdio and conio calls.
: >They were not meant to be
: >used at the same time.

: I hear many people say that, and it makes sense on an abstract level, since
: file (or stdin / out) I/O is different from console I/O.  But no one has ever
: said what concrete bad would really come of it.  ...printf("conio & stdio can't
: get along\n"); getch();...  Will this code run slowly or become unstable or be
: less portable or something?

The exact code you wrote there will do what the user expects -- print
the string and wait for a character.  The trouble with mixing stdio
and conio functions in general is that it doesn't always do what the
user wants it to.  You can mix them if you are careful, and know what
their relationship is.  I'll describe why I think it's OK for djgpp to
behave in the way it does.

[Note:  When I mention stdio here I'm talking about tty output only;
file output (I think) is fully buffered, and doesn't matter as much
anyway.]

AFAIK the conio functions are not defined by the ANSI standard.  stdio
functions are.  DJGPP's implementation of stdio (in 2.01 at least)
makes all the output line-buffered, for reasons of efficiency -- it
can do all its input/output in one go (the same reason disk write
caching is a good idea).  The ANSI standard allows this.  The buffer
will be flushed (and output) before any *stdio* input function, as I
think the standard requires.

However, stdio does not (and cannot) know about every other situation
which might cause the computer to do something before flushing the
buffer -- e.g. delays, conio input, keyboard handlers, mouse input,
etc.  There's no way to make it flush the buffer before doing all of
these.  Whilst it would be possible to tell conio to flush stdio's
buffer before getch (and other input) calls, this might be
undesirable in some situations.

The bottom line is that the stdio functions are not defined (by ANSI)
to work well when used in conjunction with the conio functions.
DJGPP's conio implementation is fairly close to Borland's, but this
doesn't mean that the stdio implementation should behave like
Borland's too.  I think the stdio functions work using DOS calls
(which is slow and so buffered), whereas the conio functions use
direct screen writes (which is faster).  If you know this then you can
decide what mixing is appropriate and what mixing is not.

I believe, though, that DJ intends to turn off stdio's line buffering
in the next release; this will cover up a lot of problems people have
had with this, but it will make output slower. :(

-- 
george DOT foot AT merton DOT oxford DOT ac DOT uk

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019