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 |
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 |