The standard error output stream cerr has unit buffering turned on by default. There is a cost for unit buffering, of course, so if an output stream is heavily used, don’t enable unit buffering unless efficiency is not a consideration.
Format fields
The second type of formatting flags work in a group. Only one of these flags can be, like the buttons on old car radios—you push one in, the rest pop out. Unfortunately this doesn’t happen automatically, and you have to pay attention to what flags you’re setting so that you don’t accidentally call the wrong setf( ) function. For example, there’s a flag for each of the number bases: hexadecimal, decimal, and octal. Collectively, these flags are referred to as the ios::basefield. If the ios::dec flag is set and you call setf(ios::hex), you’ll set the ios::hex flag, but you
Here are the flag groups and their effects:
ios::basefield | effect |
---|---|
ios::dec | Format integral values in base 10 (decimal) (the default radix—no prefix is visible). |
ios::hex | Format integral values in base 16 (hexadecimal). |
ios::oct | Format integral values in base 8 (octal). |
ios::floatfield | effect |
---|---|
ios::scientific | Display floating-point numbers in scientific format. Precision field indicates number of digits after the decimal point. |
ios::fixed | Display floating-point numbers in fixed format. Precision field indicates number of digits after the decimal point. |
"automatic" (Neither bit is set.) | Precision field indicates the total number of significant digits. |
ios::adjustfield | Effect |
---|---|
ios::left | Left-align values; pad on the right with the fill character. |
ios::right | Right-align values. Pad on the left with the fill character. This is the default alignment. |
ios::internal | Add fill characters after any leading sign or base indicator, but before the value. (In other words, the sign, if printed, is left-justified while the number is right-justified). |
Width, fill, and precision
The internal variables that control the width of the output field, the fill character used to pad an output field, and the precision for printing floating-point numbers are read and written by member functions of the same name.