The Standard C++ Iostream Library

This page is a brief overview of the C++ and libraries. In addition to documenting what should be available (according to Stroustrup), this page attempts to document the existing state of the default implementation libraries used with g++ 2.95.2. They don't always match.

Standard Streams

Several streams are automatically declared in . These include (with their types):

To support the work being done on wide characters for internationalization, there are wide streams as well:

In g++ 2.95.2, wide streams are not implemented.

Output Functions

These methods and functions are for ostreams.

Input Functions

These methods and functions are for istreams.

Additional Character and String Input

As mentioned before, the input operators for characters and strings remove leading white space. However, there are methods that allow input of characters that treat white space as characters.

Formatting

Formatting with iostreams is performed by setting flags in the stream class. There are often multiple ways of setting a particular flag.

The flags come in two types. The first type represents options that are either on or off. For example the uppercase flag is either on or off. The second type of flag represents a small set (more than 2) of options, of which only one at a time may be set. For example, the justification can be set to one of left justified, right justified, or internally justified.

All flags can be set using the setf() member function. The on/off type flags can be turned off using the unsetf member function. The small-set-option flags are changed using setf.

The setf function has two forms, one with one argument, one with two arguments. The first takes a logically or'ed combination of on/off flags. The second is used exclusively for setting the one-of-small-set flags. The first argument is the flag to be set, the second argument specifies which field of arguments the first flag belongs to.

All flags and fields belong to the class ios, which is included in all input and output streams. Consequently, they are accessed by using ios::name_of_flag or ios::name_of_field.

Base

The numeric base used to print integers can be set using the dec (base 10), oct (base 8), or hex (base 16) flags. These flags belong to the basefield field. Which ever one of these flags is set remains in effect until one of the others is set. By default, the dec flag is initially set for every stream.

For example the fragment

         cout.setf(ios::hex, ios::basefield);
         cout << 1234; 

will print out the base 16 result of 4d2.

Justification

All types can be left right or internally justified within their fields. Left and right justification do the obvious thing. Internal justification is used only with numerical values and places the sign of the number in the leftmost column of the field while right justifying the rest of the number. Internal justification is occasionally used in accounting software.

All justification flags belong to the adjustfield field. The chosen adjustment remains in effect until another adjustment is chosen. By default, all streams are initially right justified.

IO Manipulators

Many of the iostream flags can be set/unset through the use of IO manipulators which are placed directly on the stream.

The dec, oct, and hex manipulators can be placed directly on the stream. For example

         cout << hex << 1234; 

will print out the base 16 result of 4d2.