Table of Contents

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.

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

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

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