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

FlagMeaning
boolalphaBoolean values can be input/output using words.
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.
FieldValue
adjustfieldleft | right | internal
basefielddec | oct | hex
floatfieldscientific | fixed

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

    cout << boolalpha;

This is equivalent to:

    cout.setf(ios_base::boolalpha);

Manipulators defined in <ios>

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.

  • boolalpha / noboolalpha
  • showbase / noshowbase
  • showpoint / noshowpoint
  • showpos / noshowpos
  • uppercase / nouppercase
  • unitbuf / nounitbuf

Formatting controlled by a field has one manipulator for each flag, which turns off the others:

  • ios_base::adjustfield
    • internal
    • left
    • right
  • ios_base::basefield
    • dec
    • hex
    • oct
  • ios_base::floatfield
    • fixed
    • scientific

For example, the following two statements are equivalent:

   cout << left;
   cout.setf(ios_base::left, ios_base::adjustfield);

Manipulators defined in <istream>

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

Manipulators defined in <ostream>

These manipulators may only be used with output streams.

ManipulatorDescription
endlOutput a newline character and flush the stream
endsOutput a null character (does not flush)
flushFlush the stream

Manipulators defined in <iomanip>

These manipulators may be used with both input and output streams.

ManipulatorDescription
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

Field ''width'' automatically reset

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

State flags

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

FlagMeaning
ios_base::badbita fatal error has occurred
ios_base::eofbitEOF has been found
ios_base::failbita nonfatal error has occurred
ios_base::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