Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!lll-winken!ubvax!ardent!jra!jss
From: jss@jra.ardent.com (Jerry Schwarz (Compiler))
Newsgroups: comp.lang.c++
Subject: Re: Questions on I/O in 2.0
Message-ID: <8496@ardent.UUCP>
Date: 2 Oct 89 18:55:26 GMT
References: <499@bthpyd.UUCP>
Sender: news@ardent.UUCP
Reply-To: jss@jra.ardent.com (Jerry Schwarz (Compiler))
Distribution: na
Organization: Ardent Computer Corp., Sunnyvale, CA
Lines: 52

In article <499@bthpyd.UUCP> holmes@bthpyd.UUCP (Jim Holmes) writes:
>Questions about i/o under 2.0
>
>Naturally the output from the scanner is produced *along with*
>output from the various other modules.  Unfortunately, all the 
>scanner output is either printed  prior to any other output or
>it is saved up until the (iostream) output from the
>other modules is totally flushed from the buffer.  Is there
>a way to coordinate the various outputs so that things are 
>displayed in the order computed?
>

The problem is that both stdout and cout have internal buffers
that are only flushed under certain circumstances.  The simplest 
way to address the problem is to call ios::sync_with_stdio() 
before you do any output to cout.  

Ios::sync_with_stdio() does two things: 

	1. It turns cout into an ostream that uses stdout (the 
	   default writes directly to file descriptor 1) 

	2. It puts cout into unit buffering mode. This causes 
	   flushes of stdout to be done before insertions into cout
	   and flushes of cout to be done after insertions.  
	   
There is a performance penalty for doing this, which is why it 
isn't the default.  

Alternatively you can be careful about always flushing stdout 
before using cout, and always flushing cout before using stdout.

>Also, is the fact that form() lives only in  significant?
>The Book had over one page on it.  I couldn't find anything in Lippman
>about it.  Has it fallen out of favor?
>

Yes it is significant. "form" is not a good C++ interface for I/O.  
It isn't type safe and it isn't easily extensible.  If you like
a stdio interface. The same effect is easily accomplished by sprintf'ing 
into a buffer and using the char*.  (Which is all form ever did anyway.)  
The 2.0 iostream library contains a much larger variety of formatting support 
than does the 1.x stream library, so I think the need for form is much 
decreased.  

I urge people to become familiar with I/O manipulators. They are a 
little complicated to write, but once they are written, they are very 
easy to use.  And since they follow a fixed pattern, once you've 
written one its easy to write more.

Jerry Schwarz
jss@ardent.com