Stream Format States and Stream Manipulators
Various stream manipulators can be used to specify the kinds of formatting to be performed during stream-I/O operations. Stream manipulators control the output's format settings. Figure 15.12 lists each stream manipulator that controls a given stream's format state. All these manipulators belong to class ios_base. We show examples of most of these stream manipulators in the next several sections.
Stream Manipulator |
Description |
---|---|
skipws |
Skip white-space characters on an input stream. This setting is reset with stream manipulator noskipws. |
left |
Left justify output in a field. Padding characters appear to the right if necessary. |
right |
Right justify output in a field. Padding characters appear to the left if necessary. |
internal |
Indicate that a number's sign should be left justified in a field and a number's magnitude should be right justified in that same field (i.e., padding characters appear between the sign and the number). |
dec |
Specify that integers should be treated as decimal (base 10) values. |
oct |
Specify that integers should be treated as octal (base 8) values. |
hex |
Specify that integers should be treated as hexadecimal (base 16) values. |
showbase |
Specify that the base of a number is to be output ahead of the number (a leading 0 for octals; a leading 0x or 0X for hexadecimals). This setting is reset with stream manipulator noshowbase. |
showpoint |
Specify that floating-point numbers should be output with a decimal point. This is used normally with fixed to guarantee a certain number of digits to the right of the decimal point, even if they are zeros. This setting is reset with stream manipulator noshowpoint. |
uppercase |
Specify that uppercase letters (i.e., X and A tHRough F) should be used in a hexadecimal integer and that uppercase E should be used when representing a floating-point value in scientific notation. This setting is reset with stream manipulator nouppercase. |
showpos |
Specify that positive numbers should be preceded by a plus sign (+). This setting is reset with stream manipulator noshowpos. |
scientific |
Specify output of a floating-point value in scientific notation. |
fixed |
Specify output of a floating-point value in fixed-point notation with a specific number of digits to the right of the decimal point. |
15.7.1. Trailing Zeros and Decimal Points (showpoint)
Stream manipulator showpoint forces a floating-point number to be output with its decimal point and trailing zeros. For example, the floating-point value 79.0 prints as 79 without using showpoint and prints as 79.000000 (or as many trailing zeros as are specified by the current precision) using showpoint. To reset the showpoint setting, output the stream manipulator noshowpoint. The program in Fig. 15.13 shows how to use stream manipulator showpoint to control the printing of trailing zeros and decimal points for floating-point values. Recall that the default precision of a floating-point number is 6. When neither the fixed nor the scientific stream manipulator is used, the precision represents the number of significant digits to display (i.e., the total number of digits to display), not the number of digits to display after decimal point.
Figure 15.13. Controlling the printing of trailing zeros and decimal points in floating-point values.
(This item is displayed on pages 788 - 789 in the print version)
1 // Fig. 15.13: Fig15_13.cpp 2 // Using showpoint to control the printing of 3 // trailing zeros and decimal points for doubles. 4 #include 5 using std::cout; 6 using std::endl; 7 using std::showpoint; 8 9 int main() 10 { 11 // display double values with default stream format 12 cout << "Before using showpoint" << endl 13 << "9.9900 prints as: " << 9.9900 << endl 14 << "9.9000 prints as: " << 9.9000 << endl 15 << "9.0000 prints as: " << 9.0000 << endl << endl; 16 17 // display double value after showpoint 18 cout << showpoint 19 << "After using showpoint" << endl 20 << "9.9900 prints as: " << 9.9900 << endl 21 << "9.9000 prints as: " << 9.9000 << endl 22 << "9.0000 prints as: " << 9.0000 << endl; 23 return 0; 24 } // end main
|
15.7.2. Justification (left, right and internal)
Stream manipulators left and right enable fields to be left justified with padding characters to the right or right justified with padding characters to the left, respectively. The padding character is specified by the fill member function or the setfill parameterized stream manipulator (which we discuss in Section 15.7.3). Figure 15.14 uses the setw, left and right manipulators to left justify and right justify integer data in a field.
Figure 15.14. Left justification and right justification with stream manipulators left and right.
(This item is displayed on pages 789 - 790 in the print version)
1 // Fig. 15.14: Fig15_14.cpp 2 // Demonstrating left justification and right justification. 3 #include 4 using std::cout; 5 using std::endl; 6 using std::left; 7 using std::right; 8 9 #include 10 using std::setw; 11 12 int main() 13 { 14 int x = 12345; 15 16 // display x right justified (default) 17 cout << "Default is right justified:" << endl 18 << setw( 10 ) << x; 19 20 // use left manipulator to display x left justified 21 cout << " Use std::left to left justify x: " 22 << left << setw( 10 ) << x; 23 24 // use right manipulator to display x right justified 25 cout << " Use std::right to right justify x: " 26 << right << setw( 10 ) << x << endl; 27 return 0; 28 } // end main
|
Stream manipulator internal indicates that a number's sign (or base when using stream manipulator showbase) should be left justified within a field, that the number's magnitude should be right justified and that intervening spaces should be padded with the fill character. Figure 15.15 shows the internal stream manipulator specifying internal spacing (line 15). Note that showpos forces the plus sign to print (line 15). To reset the showpos setting, output the stream manipulator noshowpos.
Figure 15.15. Printing an integer with internal spacing and plus sign.
(This item is displayed on pages 790 - 791 in the print version)
1 // Fig. 15.15: Fig15_15.cpp 2 // Printing an integer with internal spacing and plus sign. 3 #include 4 using std::cout; 5 using std::endl; 6 using std::internal; 7 using std::showpos; 8 9 #include 10 using std::setw; 11 12 int main() 13 { 14 // display value with internal spacing and plus sign 15 cout << internal << showpos << setw( 10 ) << 123 << endl; 16 return 0; 17 } // end main
|
15.7.3. Padding (fill, setfill)
The fill member function specifies the fill character to be used with justified fields; if no value is specified, spaces are used for padding. The fill function returns the prior padding character. The setfill manipulator also sets the padding character. Figure 15.16 demonstrates using member function fill (line 40) and stream manipulator setfill (lines 44 and 47) to set the fill character.
Figure 15.16. Using member function fill and stream manipulator setfill to change the padding character for fields larger than the values being printed.
(This item is displayed on pages 791 - 792 in the print version)
1 // Fig. 15.16: Fig15_16.cpp 2 // Using member function fill and stream manipulator setfill to change 3 // the padding character for fields larger than the printed value. 4 #include 5 using std::cout; 6 using std::dec; 7 using std::endl; 8 using std::hex; 9 using std::internal; 10 using std::left; 11 using std::right; 12 using std::showbase; 13 14 #include 15 using std::setfill; 16 using std::setw; 17 18 int main() 19 { 20 int x = 10000; 21 22 // display x 23 cout << x << " printed as int right and left justified " 24 << "and as hex with internal justification. " 25 << "Using the default pad character (space):" << endl; 26 27 // display x with base 28 cout << showbase << setw( 10 ) << x << endl; 29 30 // display x with left justification 31 cout << left << setw( 10 ) << x << endl; 32 33 // display x as hex with internal justification 34 cout << internal << setw( 10 ) << hex << x << endl << endl; 35 36 cout << "Using various padding characters:" << endl; 37 38 // display x using padded characters (right justification) 39 cout << right; 40 cout.fill( '*' ); 41 cout << setw( 10 ) << dec << x << endl; 42 43 // display x using padded characters (left justification) 44 cout << left << setw( 10 ) << setfill( '%' ) << x << endl; 45 46 // display x using padded characters (internal justification) 47 cout << internal << setw( 10 ) << setfill( '^' ) << hex 48 << x << endl; 49 return 0; 50 } // end main
|
15.7.4. Integral Stream Base (dec, oct, hex, showbase)
C++ provides stream manipulators dec, hex and oct to specify that integers are to be displayed as decimal, hexadecimal and octal values, respectively. Stream insertions default to decimal if none of these manipulators is used. With stream extraction, integers prefixed with 0 (zero) are treated as octal values, integers prefixed with 0x or 0X are treated as hexadecimal values, and all other integers are treated as decimal values. Once a particular base is specified for a stream, all integers on that stream are processed using that base until a different base is specified or until the program terminates.
Stream manipulator showbase forces the base of an integral value to be output. Decimal numbers are output by default, octal numbers are output with a leading 0, and hexadecimal numbers are output with either a leading 0x or a leading 0X (as we discuss in Section 15.7.6, stream manipulator uppercase determines which option is chosen). Figure 15.17 demonstrates the use of stream manipulator showbase to force an integer to print in decimal, octal and hexadecimal formats. To reset the showbase setting, output the stream manipulator noshowbase.
Figure 15.17. Stream manipulator showbase.
1 // Fig. 15.17: Fig15_17.cpp 2 // Using stream manipulator showbase. 3 #include 4 using std::cout; 5 using std::endl; 6 using std::hex; 7 using std::oct; 8 using std::showbase; 9 10 int main() 11 { 12 int x = 100; 13 14 // use showbase to show number base 15 cout << "Printing integers preceded by their base:" << endl 16 << showbase; 17 18 cout << x << endl; // print decimal value 19 cout << oct << x << endl; // print octal value 20 cout << hex << x << endl; // print hexadecimal value 21 return 0; 22 } // end main
|
15.7.5. Floating-Point Numbers; Scientific and Fixed Notation (scientific, fixed)
Stream manipulators scientific and fixed control the output format of floating-point numbers. Stream manipulator scientific forces the output of a floating-point number to display in scientific format. Stream manipulator fixed forces a floating-point number to display a specific number of digits (as specified by member function precision or stream manipulator setprecision) to the right of the decimal point. Without using another manipulator, the floating-point-number value determines the output format.
Figure 15.18 demonstrates displaying floating-point numbers in fixed and scientific formats using stream manipulators scientific (line 21) and fixed (line 25). The exponent format in scientific notation might differ across different compilers.
Figure 15.18. Floating-point values displayed in default, scientific and fixed formats.
(This item is displayed on page 794 in the print version)
1 // Fig. 15.18: Fig15_18.cpp 2 // Displaying floating-point values in system default, 3 // scientific and fixed formats. 4 #include 5 using std::cout; 6 using std::endl; 7 using std::fixed; 8 using std::scientific; 9 10 int main() 11 { 12 double x = 0.001234567; 13 double y = 1.946e9; 14 15 // display x and y in default format 16 cout << "Displayed in default format:" << endl 17 << x << ' ' << y << endl; 18 19 // display x and y in scientific format 20 cout << " Displayed in scientific format:" << endl 21 << scientific << x << ' ' << y << endl; 22 23 // display x and y in fixed format 24 cout << " Displayed in fixed format:" << endl 25 << fixed << x << ' ' << y << endl; 26 return 0; 27 } // end main
|
15.7.6. Uppercase/Lowercase Control (uppercase)
Stream manipulator uppercase outputs an uppercase X or E with hexadecimal-integer values or with scientific-notation floating-point values, respectively (Fig. 15.19). Using stream manipulator uppercase also causes all letters in a hexadecimal value to be uppercase. By default, the letters for hexadecimal values and the exponents in scientific-notation floating-point values appear in lowercase. To reset the uppercase setting, output the stream-manipulator nouppercase.
Figure 15.19. Stream manipulator uppercase.
(This item is displayed on pages 794 - 795 in the print version)
1 // Fig. 15.19: Fig15_19.cpp 2 // Stream manipulator uppercase. 3 #include 4 using std::cout; 5 using std::endl; 6 using std::hex; 7 using std::showbase; 8 using std::uppercase; 9 10 int main() 11 { 12 cout << "Printing uppercase letters in scientific" << endl 13 << "notation exponents and hexadecimal values:" << endl; 14 15 // use std:uppercase to display uppercase letters; use std::hex and 16 // std::showbase to display hexadecimal value and its base 17 cout << uppercase << 4.345e10 << endl 18 << hex << showbase << 123456789 << endl; 19 return 0; 20 } // end main
|
15.7.7. Specifying Boolean Format (boolalpha)
C++ provides data type bool, whose values may be false or TRue, as a preferred alternative to the old style of using 0 to indicate false and nonzero to indicate TRue. A bool variable outputs as 0 or 1 by default. However, we can use stream manipulator boolalpha to set the output stream to display bool values as the strings "TRue" and "false". Use stream manipulator noboolalpha to set the output stream to display bool values as integers (i.e., the default setting). The program of Fig. 15.20 demonstrates these stream manipulators. Line 14 displays the bool value, which line 11 sets to true, as an integer. Line 18 uses manipulator boolalpha to display the bool value as a string. Lines 2122 then change the bool's value and use manipulator noboolalpha, so line 25 can display the bool value as an integer. Line 29 uses manipulator boolalpha to display the bool value as a string. Both boolalpha and noboolalpha are "sticky" settings.
Figure 15.20. Stream manipulators boolalpha and noboolalpha.
(This item is displayed on pages 795 - 796 in the print version)
1 // Fig. 15.20: Fig15_20.cpp 2 // Demonstrating stream manipulators boolalpha and noboolalpha. 3 #include 4 using std::boolalpha; 5 using std::cout; 6 using std::endl; 7 using std::noboolalpha; 8 9 int main() 10 { 11 bool booleanValue = true; 12 13 // display default true booleanValue 14 cout << "booleanValue is " << booleanValue << endl; 15 16 // display booleanValue after using boolalpha 17 cout << "booleanValue (after using boolalpha) is " 18 << boolalpha << booleanValue << endl << endl; 19 20 cout << "switch booleanValue and use noboolalpha" << endl; 21 booleanValue = false; // change booleanValue 22 cout << noboolalpha << endl; // use noboolalpha 23 24 // display default false booleanValue after using noboolalpha 25 cout << "booleanValue is " << booleanValue << endl; 26 27 // display booleanValue after using boolalpha again 28 cout << "booleanValue (after using boolalpha) is " 29 << boolalpha << booleanValue << endl; 30 return 0; 31 } // end main
|
Good Programming Practice 15.1
Displaying bool values as true or false, rather than nonzero or 0, respectively, makes program outputs clearer. |
15.7.8. Setting and Resetting the Format State via Member Function flags
Throughout Section 15.7, we have been using stream manipulators to change output format characteristics. We now discuss how to return an output stream's format to its default state after having applied several manipulations. Member function flags without an argument returns the current format settings as a fmtflags data type (of class ios_base), which represents the format state. Member function flags with a fmtflags argument sets the format state as specified by the argument and returns the prior state settings. The initial settings of the value that flags returns might differ across several systems. The program of Fig. 15.21 uses member function flags to save the stream's original format state (line 22), then restore the original format settings (line 30).
Figure 15.21. flags member function.
(This item is displayed on pages 796 - 797 in the print version)
1 // Fig. 15.21: Fig15_21.cpp 2 // Demonstrating the flags member function. 3 #include 4 using std::cout; 5 using std::endl; 6 using std::ios_base; 7 using std::oct; 8 using std::scientific; 9 using std::showbase; 10 11 int main() 12 { 13 int integerValue = 1000; 14 double doubleValue = 0.0947628; 15 16 // display flags value, int and double values (original format) 17 cout << "The value of the flags variable is: " << cout.flags() 18 << " Print int and double in original format: " 19 << integerValue << ' ' << doubleValue << endl << endl; 20 21 // use cout flags function to save original format 22 ios_base::fmtflags originalFormat = cout.flags(); 23 cout << showbase << oct << scientific; // change format 24 25 // display flags value, int and double values (new format) 26 cout << "The value of the flags variable is: " << cout.flags() 27 << " Print int and double in a new format: " 28 << integerValue << ' ' << doubleValue << endl << endl; 29 30 cout.flags( originalFormat ); // restore format 31 32 // display flags value, int and double values (original format) 33 cout << "The restored value of the flags variable is: " 34 << cout.flags() 35 << " Print values in original format again: " 36 << integerValue << ' ' << doubleValue << endl; 37 return 0; 38 } // end main
|