io:io_flags

C++ Reference

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.

FlagMeaning
boolalphaBoolean values can be input/output using the words “true” and “false”.
decNumeric values are displayed in decimal.
fixedDisplay floating point values using normal notation (as opposed to scientific).
hexNumeric values are displayed in hexidecimal.
internalIf a numeric value is padded to fill a field, spaces are inserted between the sign and base character.
leftOutput is left justified.
octNumeric values are displayed in octal.
rightOutput is right justified.
scientificDisplay floating point values using scientific notation.
showbaseDisplay the base of all numeric values.
showpointDisplay a decimal and extra zeros, even when not needed.
showposDisplay a leading plus sign before positive numeric values.
skipwsDiscard whitespace characters (spaces, tabs, newlines) when reading from a stream.
unitbufFlush the buffer after each insertion.
uppercaseDisplay 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>

ManipulatorDescriptionInputOutput
boolalphaTurns on the boolalpha flag X X
decTurns on the dec flag X X
endlOutput a newline character, flush the stream X
endsOutput a null character X
fixedTurns on the fixed flag X
flushFlushes the stream X
hexTurns on the hex flag X X
internalTurns on the internal flag X
leftTurns on the left flag X
noboolalphaTurns off the boolalpha flag X X
noshowbaseTurns off the showbase flag X
noshowpointTurns off the showpoint flag X
noshowposTurns off the showpos flag X
noskipwsTurns off the skipws flag X
nounitbufTurns off the unitbuf flag X
nouppercaseTurns off the uppercase flag X
octTurns on the oct flag X X
rightTurns on the right flag X
scientificTurns on the scientific flag X
showbaseTurns on the showbase flag X
showpointTurns on the showpoint flag X
showposTurns on the showpos flag X
skipwsTurns on the skipws flag X
unitbufTurns on the unitbuf flag X
uppercaseTurns on the uppercase flag X
wsSkip any leading whitespace X

Manipulators defined in <iomanip>

ManipulatorDescriptionInputOutput
resetiosflags( long f )Turn off the flags specified by f X X
setbase( int base )Sets the number base to base X
setfill( char 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:

FlagMeaning
badbita fatal error has occurred
eofbitEOF has been found
failbita nonfatal error has occurred
goodbitno errors have occurred

Mode flags

The I/O stream mode flags allow you to access files in different ways. The flags are:

ModeMeaning
ios_base::appappend output
ios_base::ateseek to EOF when opened
ios_base::binaryopen the file in binary mode
ios_base::inopen the file for reading
ios_base::outopen the file for writing
ios_base::truncoverwrite the existing file