DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
iostream examples

Interactions with stdio

The libraries differ significantly in the way they interact with stdio. The old stream header stream.h included stdio.h and some stream data structures could contain a pointer to a stdio FILE. In the iostream library specialized streams and streambufs (declared in stdiostream.h) are provided to make the connection.

The old usage:

   FILE* stdiofile ;
   filebuf fb(stdiofile) ;
   istream in(stdiofile) ;
   ostream out(stdiofile) ;
is replaced by:
   FILE* stdiofile ;
   stdiobuf fb(stdiofile) ;
   stdiostream in(stdiofile) ;
   stdiostream out(stdiofile) ;


In the old library the predefined streams cin, cout, and cerr were directly connected to the stdio FILEs stdin, stdout, and stderr. I/O was mixed character by character. Further, these streams were unbuffered in the sense that insertion and extraction was done by doing character by character puts and gets on the corresponding stdio FILEs. In the iostream library the predefined streams are attached directly to file descriptors rather than to the stdio streams. This means that for output the characters are mixed only as flushes are done and the input buffer of one is not visible to the other.

In practice the biggest problems seem to come from attempts to mix code that uses stdout with code that uses cout. The best solution is to cause flushes to be inserted whenever the program switches from one library to the other. An alternative is to use:

   ios::sync_with_stdio() ;
This causes the predefined streams to be connected to the corresponding stdio files in an unbuffered mode. The major drawback of this solution is the large overheads associated with insertion of characters in this mode. Typically insertion into cout is slowed by a factor of 4 after a call of sync_with_stdio().

The old stream library contained some ``stringifying'' functions that were called with various arguments and returned a string. These are declared in stream.h and available primarily for compatibility. The only such formatting function that seems to provide a significant functionality that is not easily available in the iostream library is form(), which allows printf() like formatting. In fact, form() is just a wrapper for calls to sprintf(). The programmer can easily write manipulators and inserters that do the same thing.


Next topic: Assignment
Previous topic: Filebuf

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005