C++ defines some format flags for standard input and output, which can be manipulated with the flags, setf, and unsetf functions. For example,
cout.setf(ios_base::left, ios_base::adjustfield);
turns on left justification for all output directed to cout.
These flags and fields are defined in the base class ios_base.
Flag | Meaning |
---|---|
boolalpha | Boolean values can be input/output using words. |
dec | Numeric values are displayed in decimal. |
fixed | Display floating point values using normal notation (as opposed to scientific). |
hex | Numeric values are displayed in hexidecimal. |
internal | If a numeric value is padded to fill a field, spaces are inserted between the sign and base character. |
left | Output is left justified. |
oct | Numeric values are displayed in octal. |
right | Output is right justified. |
scientific | Display floating point values using scientific notation. |
showbase | Display the base of all numeric values. |
showpoint | Display a decimal and extra zeros, even when not needed. |
showpos | Display a leading plus sign before positive numeric values. |
skipws | Discard whitespace characters (spaces, tabs, newlines) when reading from a stream. |
unitbuf | Flush the buffer after each insertion. |
uppercase | Display the “e” of scientific notation and the “x” of hexidecimal notation as capital letters. |
Field | Value |
---|---|
adjustfield | left | right | internal |
basefield | dec | oct | hex |
floatfield | scientific | fixed |
You can also manipulate flags indirectly, using the following manipulators. Most programmers are familiar with the endl manipulator, which might give you an idea of how manipulators are used. For example, to set the boolalpha flag, you might use the following command:
cout << boolalpha;
This is equivalent to:
cout.setf(ios_base::boolalpha);
Manipulators from <ios> may be used on both input and output streams.
Formatting controlled by a boolean has two manipulators with names matching the flag name.
Formatting controlled by a field has one manipulator for each flag, which turns off the others:
For example, the following two statements are equivalent:
cout << left; cout.setf(ios_base::left, ios_base::adjustfield);
There is one input manipulator, ws, which extracts whitespace from the stream, effectively skipping it before the next operation. This is rarely used directly, as streams with the skipws flag set should skip whitespace before any extraction. (All the standard extractors do this, user-defined extractors should use istream::sentry, which will also do this.)
These manipulators may only be used with output streams.
Manipulator | Description |
---|---|
endl | Output a newline character and flush the stream |
ends | Output a null character (does not flush) |
flush | Flush the stream |
These manipulators may be used with both input and output streams.
Manipulator | Description |
---|---|
resetiosflags( ios_base::fmtflags f ) | Turn off the flags specified by f |
setiosflags( ios_base::fmtflags f ) | Turn on the flags specified by f |
setbase( int base ) | Sets the number base to base |
setfill( char ch ) | Sets the fill character to ch |
setprecision( int p ) | Sets the number of digits of precision |
setw( int w ) | Sets the field width to w |
Unlike the other format flags and fields, the width
field is automatically set whenever the «
operator is used to output text or numbers. This can lead to surprising results. The loop
for(int i=1;i<1000;i*=10){ cout << setw(6) << i << " |" << i << endl; }
does not pad the second column as might be expected, but produces:
1 |1 10 |10 100 |100
To pad both columns, the width field should be set before each number is printed,
for(int i=1;i<1000;i*=10){ cout << setw(6) << i << " |" << setw(6) << i << endl; }
producing the more pleasing alignment:
1 | 1 10 | 10 100 | 100
Why does the standard require this extra setw(6)
? If the standard did not specify that the width
field automatically be reset, the width of the seperator ” |”
would need to be set to 0 so that extra spaces would not be added around it. If the language were defined this way, we would have to write:
for(int i=1;i<1000;i*=10){ cout << setw(6) << i << setw(0) << " |" << setw(6) << i << endl; }
The width is reset regardless of whether the function cout.width(n)
or the stream manipulator cout « setw(n)
is used. It is the only field or flag which is reset by the operator «
.
The I/O stream state flags tell you the current state of an I/O stream. The flags are:
Flag | Meaning |
---|---|
ios_base::badbit | a fatal error has occurred |
ios_base::eofbit | EOF has been found |
ios_base::failbit | a nonfatal error has occurred |
ios_base::goodbit | no errors have occurred |
The I/O stream mode flags allow you to access files in different ways. The flags are:
Mode | Meaning |
---|---|
ios_base::app | append output |
ios_base::ate | seek to EOF when opened |
ios_base::binary | open the file in binary mode |
ios_base::in | open the file for reading |
ios_base::out | open the file for writing |
ios_base::trunc | overwrite the existing file |