D(T.width(40);)
T << s << endl;
} ///:~
This example uses a trick to create a trace file so that you can monitor what’s happening. The macro D(a) uses the preprocessor "stringizing" to turn a into a string to display. Then it reiterates a so the statement is executed. The macro sends all the information to a file called T, which is the trace file. The output is:.
int i = 47;
float f = 2300114.414159;
T.setf(ios::unitbuf);
T.setf(ios::showbase);
T.setf(ios::uppercase | ios::showpos);
T << i << endl;
+47
T.setf(ios::hex, ios::basefield);
T << i << endl;
0X2F
T.setf(ios::oct, ios::basefield);
T << i << endl;
057
T.unsetf(ios::showbase);
T.setf(ios::dec, ios::basefield);
T.setf(ios::left, ios::adjustfield);
T.fill('0');
T << "fill char: " << T.fill() << endl;
fill char: 0
T.width(10);
+470000000
T.setf(ios::right, ios::adjustfield);
T.width(10);
0000000+47
T.setf(ios::internal, ios::adjustfield);
T.width(10);
+000000047
T << i << endl;
+47
T.unsetf(ios::showpos);
T.setf(ios::showpoint);
T << "prec = " << T.precision() << endl;
prec = 6
T.setf(ios::scientific, ios::floatfield);
T << endl << f << endl;
2.300114E+06
T.unsetf(ios::uppercase);
T << endl << f << endl;
2.300114e+06
T.setf(ios::fixed, ios::floatfield);
T << f << endl;
2300114.500000
T.precision(20);
T << "prec = " << T.precision() << endl;
prec = 20
T << endl << f << endl;
2300114.50000000000000000000
T.setf(ios::scientific, ios::floatfield);
T << endl << f << endl;
2.30011450000000000000e+06
T.setf(ios::fixed, ios::floatfield);
T << f << endl;
2300114.50000000000000000000
T.width(10);
Is there any more?
T.width(40);
0000000000000000000000Is there any more?
T.setf(ios::left, ios::adjustfield);
T.width(40);
Is there any more?0000000000000000000000
Studying this output should clarify your understanding of the iostream formatting member functions.
Manipulators
As you can see from the previous program, calling the member functions for stream formatting operations can get a bit tedious. To make things easier to read and write, a set of manipulators is supplied to duplicate the actions provided by the member functions. Manipulators are a convenience because you can insert them for their effect within a containing expression; you don’t have to create a separate function-call statement.
Manipulators change the state of the stream instead of (or in addition to) processing data. When you insert endl in an output expression, for example, it not only inserts a newline character, but it also flushes the stream (that is, puts out all pending characters that have been stored in the internal stream buffer but not yet output). You can also just flush a stream like this:.
cout << flush;
which causes a call to the flush( ) member function, as in
cout.flush();
as a side effect (nothing is inserted into the stream). Additional basic manipulators will change the number base to oct (octal), dec (decimal) or hex (hexadecimal):.
cout << hex << "0x" << i << endl;
In this case, numeric output will continue in hexadecimal mode until you change it by inserting either dec or oct in the output stream.
There’s also a manipulator for extraction that "eats" white space:.
cin >> ws;
Manipulators with no arguments are provided in <iostream>. These include dec, oct, and hex , which perform the same action as, respectively, setf(ios::dec, ios::basefield), setf(ios::oct, ios::basefield), and setf(ios::hex, ios::basefield), albeit more succinctly. The <iostream> header also includes ws, endl, and flush and the additional set shown here:.
Manipulator | Effect |
---|---|
showbase noshowbase | Indicate the numeric base (dec, oct, or hex) when printing an integral value. The format used can be read by the C++ compiler. |
showpos noshowpos | Show plus sign (+) for positive values. |
uppercase nouppercase | Display uppercase A-F for hexadecimal values, and display E for scientific values. |
showpoint noshowpoint | Show decimal point and trailing zeros for floating-point values. |
skipws noskipws | Skip white space on input. |
left right internal | Left-align, pad on right. Right-align, pad on left. Fill between leading sign or base indicator and value. |
scientific fixed | Indicates the display preference for floating-point output (scientific notation vs. fixed-point decimal). |