www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1999/05/09/17:21:26

From: Alain Magloire <alainm AT rcsm DOT ece DOT mcgill DOT ca>
Message-Id: <199905092116.RAA26138@spock2.ECE.McGill.CA>
Subject: Re: Quirky getch() (fwd)
To: djgpp-workers AT delorie DOT com
Date: Sun, 9 May 1999 17:16:08 -0400 (EDT)
In-Reply-To: <Pine.SUN.3.91.990509141640.11133A-100000@is> from "Eli Zaretskii" at May 9, 99 02:30:28 pm
X-Mailer: ELM [version 2.4 PL25]
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

Bonjour

> > The thought that every
> > call to getch() would be preceded by automatic calls to fflush(stdout)
> > and fflush(stderr) is chilling indeed.
> 
> Why is it more chilling than what we already do?

stderr is an unbuffered stream, you should not have to do fflush(stderr).

But what is getch ?

This is code is wrong :

int main ()
{
  FILE * f = fopen (...);
  ...
  fwrite (..., f);
  write (fileno(f), ...);
  ...
  fwrite (..., f);
  write (fileno(f), ...);
}

This is incorrect meaning that the stream will intertwine.
It is obvious that mixing two APIs, stdio and basic I/O will
make the flow of data inconsistent, unless you fflush() between each call or
use setvbuf()/setbuf() to unbuffered.

And it's the same thing here :
...
int main() {
      clrscr();
      cout << "Hello World\n";
      cout << "Sing along!";
      getch();
      return 0;
}           

You're using two different APIs again, Iostreams and stdio(getch) or
whatever API getch() is part of.  If I do

cout << "Hello World\n";
cout << Sing Aling !";
read (0, &c, 1);

I don't think you'll modify read() to fflush().
I don't have my C++ std with me but the correct/portable approach
should have been to, cout.flush() or to use endl or simply to
use cin.  cout and cin are tied in the lib.

my 0.02$ canadian/Euro, is that you have many different APIs for I/O:
stdio, iostream, basic I/O, getch.  If you mix them you should be
very carefull, each have they're own bufferring, or simply stick
to one.
--
alain

- Raw text -


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