Information About IOstreams


This page contains some information about C++ iostreams. This page is under construction and doesn't contain many information yet...

Writing integers in different formats

It is often needed to write an int to a stream in a format different from decimal. To do this some manipulators are defined:
dec
switch to decimal mode (the default mode)
oct
switch to octal mode (numbers are preceeded by (0)
hex
switch to hexadecimal mode (numbers are preceeded by '0x')
Note that the mode isn't reset after printing an integer. It has to be reset manually.
Here is an example use:
      int	i;
      cout << "dec: " << i << " oct: " << oct << i << " hex: " << hex << i << endl;
    
There is no standard method to print numbers in binary format. To do this a specialized manipulator can be implemented (see iobinary.cc). This could be used as follows:
      int	i;
      cout << "binary: " << bin(i) << endl;
    
Naturally, this can't introduce a binary mode. Thus the 'bin(.)' has to be used everytime an int should be printed in binary format.

Writing to a Specialized IOstream

Sometimes the problem occures that some output should be send to a special output device e.g. a certain window of an application. This can easily be done with iostreams: all which is needed is to write a class derived from streambuf. The derived class implements an interface to the window. Here is an example how to implement this: The needed declarations are found in wstream.h: The declaration of a class derived from streambuf (widgetbuf) and a declaration of a class derived from ostream (owstream). The corresponding implementation is located in wstream.cc. A completely uncommented application of the owstream is found in wstreamtst.cc. However, although it is uncommented it should be quite obvious what happens...

Another example of a specialized IOstream, which doesn't use any platform specific library, is a stream which prefixes every line written with a prefix string and strips off a prefix from every line read. The declaration of such an IOstream is found in prfxstream.h, the implementation of the member functions is found in prfxstream.cc, and an (uncommented) application of this stream is presented in prfxtst.cc.

Since I wrote the above mentioned streambufs, I have implemented several others, too. Some of them are available via ftp from ftp://ftp.informatik.uni-konstanz.de/pub/algo/personal/kuehl/sbufs.tgz. This is a (slowly) growing collection of streambufs for special purposes.


Dietmar Kühl: Dietmar.Kuehl@uni-konstanz.de