| Bruce Eckel's Thinking in C++, 2nd Ed | Contents | Prev | Next |
fmtflags ios::flags(fmtflags newflags); fmtflags ios::setf(fmtflags ored_flag);
|
on/off
flag
|
effect
|
|---|---|
|
Skip
white space. (For input; this is the default.)
|
|
|
Indicate
the numeric base (dec, oct, or hex) when printing an integral value. The format
used can be read by the C++ compiler.
|
|
|
Show
decimal point and trailing zeros for floating-point values.
|
|
|
Display
uppercase A-F for hexadecimal values and E for scientific values.
|
|
|
Show
plus sign (+) for positive values.
|
|
|
“Unit
buffering.” The stream is flushed after each insertion.
|
|
|
Synchronizes
the stream with the C standard I/O system.
|
|
ios::basefield
|
effect
|
|---|---|
|
Format
integral values in base 10 (decimal) (default radix).
|
|
|
Format
integral values in base 16 (hexadecimal).
|
|
|
Format
integral values in base 8 (octal).
|
|
ios::floatfield
|
effect
|
|---|---|
|
Display
floating-point numbers in scientific format. Precision field indicates number
of digits after the decimal point.
|
|
|
Display
floating-point numbers in fixed format. Precision field indicates number of
digits after the decimal point.
|
|
|
Precision
field indicates the total number of significant digits.
|
|
ios::adjustfield
|
effect
|
|---|---|
|
Left-align
values; pad on the right with the fill character.
|
|
|
Right-align
values. Pad on the left with the fill character. This is the default alignment.
|
|
|
Add
fill characters after any leading sign or base indicator, but before the value.
|
|
function
|
effect
|
|---|---|
|
Reads
the current width. (Default is 0.) Used for both insertion and extraction.
|
|
|
int
ios::width(int n)
|
Sets
the width, returns the previous width.
|
|
Reads
the current fill character. (Default is space.)
|
|
|
int
ios::fill(int n)
|
Sets
the fill character, returns the previous fill character.
|
|
Reads
current floating-point precision. (Default is 6.)
|
|
|
int
ios::precision(int n)
|
Sets
floating-point precision, returns previous precision. See
ios::floatfield
table for the meaning of “precision.”
|
//: C18:Format.cpp
// Formatting functions
#include <fstream>
using namespace std;
#define D(A) T << #A << endl; A
ofstream T("format.out");
int main() {
D(int i = 47;)
D(float f = 2300114.414159;)
char* s = "Is there any more?";
D(T.setf(ios::unitbuf);)
// D(T.setf(ios::stdio);) // SOMETHING MAY HAVE CHANGED
D(T.setf(ios::showbase);)
D(T.setf(ios::uppercase);)
D(T.setf(ios::showpos);)
D(T << i << endl;) // Default to dec
D(T.setf(ios::hex, ios::basefield);)
D(T << i << endl;)
D(T.unsetf(ios::uppercase);)
D(T.setf(ios::oct, ios::basefield);)
D(T << i << endl;)
D(T.unsetf(ios::showbase);)
D(T.setf(ios::dec, ios::basefield);)
D(T.setf(ios::left, ios::adjustfield);)
D(T.fill('0');)
D(T << "fill char: " << T.fill() << endl;)
D(T.width(10);)
T << i << endl;
D(T.setf(ios::right, ios::adjustfield);)
D(T.width(10);)
T << i << endl;
D(T.setf(ios::internal, ios::adjustfield);)
D(T.width(10);)
T << i << endl;
D(T << i << endl;) // Without width(10)
D(T.unsetf(ios::showpos);)
D(T.setf(ios::showpoint);)
D(T << "prec = " << T.precision() << endl;)
D(T.setf(ios::scientific, ios::floatfield);)
D(T << endl << f << endl;)
D(T.setf(ios::fixed, ios::floatfield);)
D(T << f << endl;)
D(T.setf(0, ios::floatfield);) // Automatic
D(T << f << endl;)
D(T.precision(20);)
D(T << "prec = " << T.precision() << endl;)
D(T << endl << f << endl;)
D(T.setf(ios::scientific, ios::floatfield);)
D(T << endl << f << endl;)
D(T.setf(ios::fixed, ios::floatfield);)
D(T << f << endl;)
D(T.setf(0, ios::floatfield);) // Automatic
D(T << f << endl;)
D(T.width(10);)
T << s << endl;
D(T.width(40);)
T << s << endl;
D(T.setf(ios::left, ios::adjustfield);)
D(T.width(40);)
T << s << endl;
D(T.unsetf(ios::showpoint);)
D(T.unsetf(ios::unitbuf);)
// D(T.unsetf(ios::stdio);) // SOMETHING MAY HAVE CHANGEDint i = 47;
float f = 2300114.414159;
T.setf(ios::unitbuf);
T.setf(ios::stdio);
T.setf(ios::showbase);
T.setf(ios::uppercase);
T.setf(ios::showpos);
T << i << endl;
+47
T.setf(ios::hex, ios::basefield);
T << i << endl;
+0X2F
T.unsetf(ios::uppercase);
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.300115e+06
T.setf(ios::fixed, ios::floatfield);
T << f << endl;
2300114.500000
T.setf(0, ios::floatfield);
T << f << endl;
2.300115e+06
T.precision(20);
T << "prec = " << T.precision() << endl;
prec = 20
T << endl << f << endl;
2300114.50000000020000000000
T.setf(ios::scientific, ios::floatfield);
T << endl << f << endl;
2.30011450000000020000e+06
T.setf(ios::fixed, ios::floatfield);
T << f << endl;
2300114.50000000020000000000
T.setf(0, ios::floatfield);
T << f << endl;
2300114.50000000020000000000
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
T.unsetf(ios::showpoint);
T.unsetf(ios::unitbuf);