cppreference.com > I/O Flags

C++ I/O Flags

Format flags

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

turns on left justification for all output directed to cout.

Flag Meaning
boolalpha Boolean values can be input/output using the words "true" and "false".
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.

Manipulators

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 dec flag, you might use the following command:

  cout << dec;
Manipulators defined in <iostream>
Manipulator Description Input Output
boolalpha Turns on the boolalpha flag X X
dec Turns on the dec flag X X
endl Output a newline character, flush the stream X
ends Output a null character X
fixed Turns on the fixed flag X
flush Flushes the stream X
hex Turns on the hex flag X X
internal Turns on the internal flag X
left Turns on the left flag X
noboolalpha Turns off the boolalpha flag X X
noshowbase Turns off the showbase flag X
noshowpoint Turns off the showpoint flag X
noshowpos Turns off the showpos flag X
noskipws Turns off the skipws flag X
nounitbuf Turns off the unitbuf flag X
nouppercase Turns off the uppercase flag X
oct Turns on the oct flag X X
right Turns on the right flag X
scientific Turns on the scientific flag X
showbase Turns on the showbase flag X
showpoint Turns on the showpoint flag X
showpos Turns on the showpos flag X
skipws Turns on the skipws flag X
unitbuf Turns on the unitbuf flag X
uppercase Turns on the uppercase flag X
ws Skip any leading whitespace X
Manipulators defined in <iomanip>
Manipulator Description Input Output
resetiosflags( long f ) Turn off the flags specified by f X X
setbase( int base ) Sets the number base to base X
setfill( int ch ) Sets the fill character to ch X
setiosflags( long f ) Turn on the flags specified by f X X
setprecision( int p ) Sets the number of digits of precision X
setw( int w ) Sets the field width to w X

State flags

The I/O stream state flags tell you the current state of an I/O stream. The flags are:

Flag Meaning
badbit a fatal error has occurred
eofbit EOF has been found
failbit a nonfatal error has occurred
goodbit no errors have occurred

Mode flags

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