In C++, there are 2 related but conceptually different ways to format data. One is by setting various format status flags defined inside the ios class or by calling ios member functions, and the other is by using special functions called stream manipulators that can be included as part of an I/O expression.
Flags Interpretation/Example
1. skipws discard leading white-space characters when reading input
cin >> setiosflags(ios::skipws) >> x;
2. left left-justify the display of the next item in the output stream
cout << setiosflags(ios::left) << x;
3. right right-justify the display of the next item in the output stream
cout << setiosflags(ios::right) << x;
4. internal pad the following numeric value with spaces inserted between any sign or base character,
and display the sign left-justified and the magnitude right-justified
cout << setiosflags(ios::internal) << x;
5. dec display or read the next value in base 10
cout << setiosflags(ios::dec) << x;
cin >> setiosflags(ios::dec) >> x;
6. oct display or read the next value in base 8
cout << setiosflags(ios::oct) << x;
cin >> setiosflags(ios::dec) >> x;
7. hex display or read the next value in base 16
cout << setiosflags(ios::hex) << x;
cin >> setiosflags(ios::hex) >> x;
8. showbase show the base of the next numeric value(e.g., 1F as 0x1F)
cout << setiosflags(ios::showbase) << x;
9. uppercase display the characters in the following value in uppercase
cout << setiosflags(ios::uppercase) << x;
10. showpos precede the display of the next value with a '+' sign
cout << setiosflags(ios::showpos) << x;
11. scientific show the following floating-point value in scientific notation
cout << setiosflags(ios::scientific) << x;
12. fixed display the following value as floating-point, even if fraction is 0 (zero)
cout << setiosflags(ios::fixed) << x;
13. unitbuf flush the C++ I/O system after each output operation
cout << setiosflags(ios::unitbuf) << x;
14. boolalpha display the following boolean value // not yet supported in the ios class
cout << setiosflags(ios::boolalpha) << x;
main( )
{
cout.setf(ios::hex);
cout.setf(ios::showbase); // or cout.setf(ios::hex | ios::showbase);
cout << 100; // displays 0x64
}
format: long setf(long flags1, long flags2); (flag2's are cleared, then flag1's are set if they also
appeared in flags2)
main( )
{
cout.setf(ios::showpos | ios::showpoint); // set both flags
cout << 10.00 << '\n'; // displays +10.000000
cout.setf(ios::showpoint, ios::showpos | ios::showpoint); // clear showpoint and showpos, and
// then reset showpoint
cout << 10.00 // showpos = 0, showpoint = 1 à display 10.000000
}
the designated flags and return the value of the flags prior to being reset, as a 'long' value:
main( )
{
cout.setf(ios::uppercase | ios::scientific);
cout << 100.12; // displays 1.0012E+02
cout.unsetf(ios::uppercase); // clear the 'uppercase' flag
cout << '\n' << 100.12; // displays 1.0012e+02
}
#include<iostream.h>
main( )
{
long f = 0x04A4;
cout.flags(f);
…
}
1. setw set the display width of the following value
cout << setw(4) << x;
2. setiosflags set the flags in the ios class according to the parameters
cout << setiosflags(ios::fixed | ios::showpoint);
cin >> setiosflags(ios::ws | ios::dec);
3. setprecision set the display precision of the next value
cout << setprecision(2);
4. setfill fill all white spaces with the character argument
cout << setfill('a');
5. resetiosflags turn off the flags specified in the parameter (input, output)
cout << resetiosflags(ios::showpos | ios::showpoint);
cin >> resetiosflags(ios::ws | ios::hex);
6. setbase set the base specified in the parameter
cout << setbase(16);
Function Function = Stream manipulator
A. width(x); cout.width(4); cout << setw(4);
cin.width(4); cin >> setw(4);
B. precision(x); cout.precision(2); cout << setprecision(2);
C. fill('a'); cout.fill('a'); cout << setfill('a');
The duration of any member function or stream manipulator is compiler-dependent per the 1996 ANSI C++ standards. Check the compiler's manuals.