Rogue Wave banner
Previous fileTop of DocumentContentsIndexNext file

7.3 Format Control Using the Stream's Format State

7.3.1 Format Parameters

Associated with each stream are a number of format state variables that control the details of formatting and parsing. Format state variables are classes inherited from a stream's base class, either ios_base or basic_ios<charT,traits>. There are two kinds of format parameters: those that can have an arbitrary value, and those that can have only a few different values.

7.3.1.1 Parameters That Can Have an Arbitrary Value

The value is stored as a private data member in one of the base classes, and set and retrieved through public member functions inherited from that base class. There are three such parameters, described in Table 6:

Table 6 -- Format parameters with arbitrary values

Access functionDefined in base classEffectDefault 
width()
ios_base
Minimal field width
0
precision()
ios_base
Precision of floating point values
6
fill()
basic_ios
<charT,traits>
Fill character for padding
The space character

7.3.1.2 Parameters That Can Have Only a Few Different Values

Typically, these would have just two or three different values. These parameters are represented by one or more bits in a data member of type fmtflags in class ios_base. These are usually called format flags. You can set format flags using the setf() function in class ios_base, clear them using unsetf(), and retrieve them through the flags() function.

Some format flags are grouped because they are mutually exclusive; for example, output within an output field can be adjusted to the left or to the right, or to an internally specified adjustment. One and only one of the corresponding three format flags, left, right, or internal, can be set.2 If you want to set one of these bits to 1, you need to set the other two to 0. To make this easier, there are bit groups whose main function is to reset all bits in one group. The bit group for adjustment is adjustfield, defined as left | right | internal.

Table 7 gives an overview of all format flags and their effects on input and output operators. (For details on how the format flags affect input and output operations, see the Class Reference entry for ios_base.) The first column below, format flag, lists the flag names; for example, showpos stands for ios_base::showpos. The group column lists the name of the group for flags that are mutually exclusive. The third column gives a brief description of the effect of setting the flag. The stdio column refers to format characters used by the C functions scanf() or printf() that have the same or similar effect. The last column, default, lists the setting that is used if you do not explicitly set the flag.

Table 7 -- Flags and their effects on operators

Format flagGroupEffectstdio Default 
  adjustfield
Adds fill characters to certain generated output for adjustment:

left
See footnote 1
left
left
-
 
right
right
0
 
internal
Adds fill characters at designated internal point
none
 
  basefield
Converts integer input or generates integer output in:
%i
dec
dec
decimal base
%d,%u
 
oct
octal base
%o
 
hex
hexadecimal base
%x
 
  floatfield
Generates floating point output:
%g,%G
fixed
fixed
in fixed-point notation
%f
 
scientific
in scientific notation
%e,%E
 
boolalpha
  Inserts and extracts bool values in alphabetic format
  0
showpos
Generates a + sign in non-negative generated numeric output
+
0
showpoint
Always generates a decimal-point in generated floating-point output
.n
See footnote 2
0
showbase
Generates a prefix indicating the numeric base of a generated integer output
#
0
skipws
Skips leading white space before certain input operations
none
1
unitbuf
Flushes output after each formatting operation
none
0
uppercase
Replaces certain lowercase letters with their uppercase equivalents in generated output
%X
%E
%G
0
1Initially, none of the bits is set. This is more or less equivalent to left.
2Where n indicates the number of decimals

The effect of setting a format parameter is usually permanent; that is, the parameter setting is in effect until the setting is explicitly changed. The only exception to this rule is the field width. The width is automatically reset to its default value, 0, after each input or output operation that uses the field width. Here is an example:

//1Extracting an integer is independent of the specified field width. The extractor for integers always reads as many digits as belong to the integer. As extraction of integers does not use the field width setting, the field width of 10 is still in effect when a character sequence is subsequently extracted. Only 10 characters are extracted in this case. After the extraction, the field width is reset to 0.
//2The inserter for integers uses the specified field width and fills the field with padding characters if necessary. After the insertion, it resets the field width to 0. Hence, the subsequent insertion of the string does not fill the field with padding characters for a string with less than 10 characters.

NOTE: With the exception of the field width, all format parameter settings are permanent. The field width parameter is reset after each use.

The following code sample shows how you can control formatting by using some of the parameters:

//1Store the current format flag setting, in order to restore it later on.
//2Change the adjustment from the default setting right to left.
//3Set the field width from its default 0 to 10. A field width of 0 means that no padding characters are inserted, and this is the default behavior of all insertions.
//4Clear the adjustment flags.
//5Change the precision for floating-point values from its default 6 to 2, and set yet another couple of format flags that affect floating-point values.
//6Restore the original flags.

The output is:

7.3.2 Manipulators

Format control requires calling a stream's member functions. Each such call interrupts the respective shift expression. But what if you need to change formats within a shift expression? This is possible in iostreams. Instead of writing:

you can write:

In this example, objects like left, setw, and endl are called manipulators. A manipulator is an object of a certain type; let's call the type manip for the time being. There are overloaded versions of:

and:

for type manip. Hence a manipulator can be extracted from or inserted into a stream together with other objects that have the shift operators defined. (Section 7.3.2 explains in greater detail how manipulators work and how you can implement your own manipulators.)

The effect of a manipulator need not be an actual input to or output from the stream. Most manipulators set just one of the above described format flags, or do some other kind of stream manipulation. For example, an expression like:

is equivalent to:

Nothing is inserted into the stream. The only effect is that the format flag for adjusting the output to the left is set.

On the other hand, the manipulator endl inserts the newline character to the stream, and flushes to the underlying stream buffer. The expression:

is equivalent to:

Some manipulators take arguments, like setw(int). The setw manipulator sets the field width. The expression:

is equivalent to:

In general, you can think of a manipulator as an object you can insert into or extract from a stream, in order to manipulate that stream. Some manipulators can be applied only to output streams, others only to input streams. Most manipulators change format bits only in one of the stream base classes, ios_base or basic_ios<charT,traits>. These can be applied to input and output streams.

Table 8 gives an overview of all manipulators defined by iostreams. The first column, Manipulator, lists its name. All manipulators are classes defined in the namespace ::std. The second column, Use, indicates whether the manipulator is intended to be used with istreams (i), ostreams (o), or both (io). The third column, Effect, summarizes the effect of the manipulator. The last column, Equivalent, lists the corresponding call to the stream's member function.

Note that the second column indicates only the intended use of a manipulator. In many cases, it is possible to apply an output manipulator to an input stream, and vice versa. Generally, this kind of non-intended manipulation is harmless in that it has no effect. For instance, if you apply the output manipulator showpoint to an input stream, the manipulation is simply ignored. However, if you use an output manipulator on a bidirectional stream during input, the manipulation does not affect current input operations, but subsequent output operations.

Table 8 -- Manipulators1

ManipulatorUseEffectEquivalent 
boolalpha
io
Puts bool values in alphabetic format
io.setf(ios_base::boolalpha)
dec
io
Converts integers to/from decimal notation
io.setf(ios_base::dec,
ios_base::basefield)
endl
o
Inserts newline and flushes buffer
o.put(o.widen('\n'));
o.flush()
ends
o
Inserts end of string character
o.put(o.widen('\0'))
fixed
o
Puts floating point values in fixed-point notation
o.setf(ios_base::fixed,
ios_base::floatfield)
flush
o
Flushes stream buffer
o.flush()
hex
io
Converts integers to/from hexadecimal notation
io.setf(ios_base::hex,
ios_base::basefield)
internal
o
Adds fill characters at a designated internal point
o.setf(ios_base::internal,
ios_base::adjustfield)
left
o
Adds fill characters for adjustment to the left
o.setf(ios_base::left,
ios_base::adjustfield)
noboolalpha
io
Resets the above
io.unsetf(ios_base::boolalpha)
noshowbase
o
Resets the above
o.unsetf (ios_base::showbase)
noshowpoint
o
Resets the above
o.unsetf (ios_base::showpoint)
noshowpos
o
Resets the above
o.unsetf (ios_base::showpos)
noskipws
i
Resets the above
i.unsetf(ios_base::skipws)
nounitbuf
o
Resets the above
o.unsetf(ios_base::unitbuf)
nouppercase
  Resets the above
o.unsetf (ios_base::uppercase)
oct
io
Converts to/from octal notation
io.setf(ios_base::oct,
ios_base::basefield)
resetiosflags
(ios_base::fmt
flags mask)
io
Clears ios flags
io.setf((ios_base::fmtflags)
0, mask)
right
o
Adds fill characters for adjustment to the right
o.setf(ios_base::right,
ios_base::adjustfield)
scientific
  Puts floating point values in scientific notation
o.setf(ios_base::scientific,
ios_base::floatfield)
setbase
(int base)
io
Sets base for integer notation (base = 8, 10, 16)
io.setf (base ==
8?ios_base::oct: base == 10
? ios_base::dec : base == 16
? ios_base::hex :
ios_base::fmtflags(0),
ios_base::basefield)
setfill(charT c)
io
Sets fill character for padding
io.fill(c)
setiosflags
(ios_base::fmt
flags mask)
io
Sets ios flags
io.setf(mask)
setprecision
(int n)
io
Sets precision of floating point values
io.precision(n)
setw(int n)
io
Sets minimal field width
io.width(n)
showbase
o
Generates a prefix indicating the numeric base of an integer
o.setf(ios_base::showbase)
showpoint
o
Always generates a decimal-point for floating-point values
o.setf(ios_base::showpoint)
showpos
o
Generates a + sign for non-negative numeric values
o.setf(ios_base::showpos)
skipws
i
Skips leading white space
i.setf(ios_base::skipws)
unitbuf
o
Flushes output after each formatting operation
o.setf(ios_base::unitbuf)
uppercase
o
Replaces certain lowercase letters with their uppercase equivalents
o.setf(ios_base::uppercase)
ws
i
Skips white spaces
 
1The Rogue Wave implementation of the Standard C++ Library also specifies the non-standard manipulators:
__lock locks the stream for multithread use;
__unlock unlocks the stream for multithread use.


Previous fileTop of DocumentContentsIndexNext file

OEM Edition, ©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.