iostream examples

Incore formatting

In the stream library the use of arrays of characters as sources or sinks was supported as the default behavior of streambuf. Although some attempt to preserve the default behavior is made by the iostream library these uses of a streambuf are considered obsolete. The support of incore operations is specifically the responsibility of the strstreambuf declared in strstream.h. streambufs created for this purpose can usually be replaced directly by strstreambufs that have equivalent behavior. The stream usage:

   char* buf[10] ;
   streambuf b(buf,10) ;

is equivalent to the iostream:
   char* buf[10] ;
   strstreambuf b(buf,10) ;

and the old method for initializing a streambuf for extraction:
   char* buf[10] ;
   streambuf b ;
   b.setbuf(buf,10,buf+5) ;

is equivalent to the iostream method:
   char* buf[10] ;
   strstreambuf b(buf,10,buf+5) ;

Frequently these uses of streambuf do not appear explicitly in the program but are the consequence of using certain constructors of istream and ostream. These constructors are supplied in the iostream library, but are considered obsolete. The equivalent forms using strstream should be used.

The old method of storing a formatted value into an array:

   char* buf[10] ;
   ostream out(10,b) ;

is replaced by:
   char* buf[10] ;
   ostrstream out(b,10) ;

Note that the order of the arguments is reversed. The new order creates more consistency between various uses of strstreams.

The old method of extracting a formatted value from an array:

   char* buf[10] ;
   istream in(10,b) ;

is replaced by
   char* buf[10] ;
   istrstream in(b,10) ;

The old istream() constructor allowed an optional extra argument to specify skipping of whitespace. In the iostream library this is part of a greatly expanded collection of state variables and so an extra argument is not provided for the istrstream() constructor. However, the obsolete form of istream() constructor continues to accept these optional arguments.

Next topic: Filebuf
Previous topic: streambuf internals

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004