The function
setf has two
overloaded versions shown below. While
unsetf just clears the specified bits.
setf( flagvalues) ;
setf( flagvalues, maskvalues) ;
unsetf( flagvalues) ;
The variable flags is derived by
ORing together all the bits you want with |. So if you want
scientific, uppercase and boolalpha then use this. Only the bits passed in as the
parameter are set. The other bits are left unchanged.
cout.setf( ios_base::scientific | ios_base::uppercase | ios_base::boolalpha) ;
cout << hex << endl;
cout << 1234 << endl;
cout << dec << endl;
cout << 123400003744.98765 << endl;
bool value=true;
cout << value << endl;
cout.unsetf( ios_base::boolalpha) ;
cout << value << endl;
Produces
4D2
1.234000E+011
true
1
Masking Bits
The two
parameter version of setf uses a
mask. If the bit is set in both the first and second parameters then it gets set. If the bit is only in the second parameter then it is cleared. The values
adjustfield, basefield and
floatfield (listed below) are composite flags, that is several flags
Or'd together. For
basefield with the values
0x0e00 is the same as
dec | oct | hex. So
setf( ios_base::hex,ios_basefield ) ;
clears all three flags then sets
hex. Similarly
adjustfield is
left | right | internal and
floatfield is
scientific | fixed.
List of Bits
This list of enums is taken from Microsoft Visual C++ 6.0. The actual values used are arbitrary- another compiler may use different values.
skipws = 0x0001
unitbuf = 0x0002
uppercase = 0x0004
showbase = 0x0008
showpoint = 0x0010
showpos = 0x0020
left = 0x0040
right = 0x0080
internal = 0x0100
dec = 0x0200
oct = 0x0400
hex = 0x0800
scientific = 0x1000
fixed = 0x2000
boolalpha = 0x4000
adjustfield = 0x01c0
basefield = 0x0e00,
floatfield = 0x3000
_Fmtmask = 0x7fff,
_Fmtzero = 0
On the next Page : About clog and cerr