Tekson

禧之狼

博客园 首页 联系 订阅 管理

 

ios_base Class

The class describes the storage and member functions common to both input and output streams that do not depend on the template parameters.

Note, base means 进制 or 基数 in Chinese; basefield represents bin, oct, dec, hex; showbase means writing integral values preceded by their corresponding numeric base prefix.

An object of class ios_base stores formatting information, which consists of:

·         Format flags in an object of type fmtflags.

·         An exception mask in an object of type iostate.

1 ios_base’s member variables

1.1 ios_base::fmtflags

typedef T1 fmtflags; 

static const fmtflags boolalpha, dec, fixed, hex, internal, left, oct, right, scientific, showbase, showpoint, showpos, skipws, unitbuf, uppercase, adjustfield, basefield, floatfield;

The type is a bitmask type T1 that describes an object that can store format flags. The distinct flag values (elements) are:

In addition, several useful values are:

field

member constant

effect when set

independent flags

boolalpha

read/write bool elements as alphabetic strings (true and false).

showbase

write integral values preceded by their corresponding numeric base prefix.

showpoint

write floating-point values including always the decimal point.

showpos

write non-negative numerical values preceded by a plus sign (+).

skipws

skip leading whitespaces on certain input operations.

unitbuf

flush output after each inserting operation.

uppercase

write uppercase letters replacing lowercase letters in certain insertion operations.

numerical base
(
basefield)

dec

read/write integral values using decimal base format.

hex

read/write integral values using hexadecimal base format.

oct

read/write integral values using octal base format.

float format
(
floatfield)

fixed

write floating point values in fixed-point notation.

scientific

write floating-point values in scientific notation.

adjustment
(
adjustfield)

internal

the output is padded to the field width by inserting fill characters at a specified internal point.

left

the output is padded to the field width appending fill characters at the end.

right

the output is padded to the field width by inserting fill characters at the beginning.

·         adjustfield, a bitmask defined as internal | left | right

·         basefield, defined as dec | hex | oct

·         floatfield, defined as fixed | scientific

1.2 ios_base::iostate

typedef T2 iostate; 

static const iostate badbit, eofbit, failbit, odbit;

The type is a bitmask type T2 that describes an object that can store stream state information. The distinct flag values (elements) are:

·         badbit, to record a loss of integrity of the stream buffer.

·         eofbit, to record end-of-file while extracting from a stream.

·         failbit, to record a failure to extract a valid field from a stream.

In addition, a useful value is goodbit, where no bits are set.

2 ios_base’s member functions

2.1 ios_base::flags

fmtflags flags( ) const; 

fmtflags flags(fmtflags _Fmtfl);

The first member function returns the stored format flags. The second member function stores _Fmtfl in the format flags and returns its previous stored value.

int main ( )

{

     using namespace std;

     cout << cout.flags() << endl; //513

     cout.flags(ios_base::dec | ios_base::boolalpha); //16896

     cout << cout.flags() << endl;

     return 0;

}

2.2 ios_base::setf

void setf(fmtflags _Mask); //_Mask is the independent flags as following table.

fmtflags setf(fmtflags _Mask, fmtflags _Unset); //_Mask is the dependent on _Unset, and the the former flag will be set as latter property.

The first member function effectively calls flags(_Mask | _Flags) (set selected bits) and then returns the previous format flags. The second member function effectively calls flags(_Mask & fmtfl, flags & ~_Mask) (replace selected bits under a mask) and then returns the previous format flags.

int main( )

{

     int i = 10;

     cout << i << endl;//10

     cout.setf(ios_base::hex, ios_base::basefield);//set hex as the basefield

     //hex is dependent on the basefield flag, so here is the second parameter.

     cout << i << endl;//a

     return 0;

}

2.3 ios_base::unsetf

void unsetf(fmtflags _Mask); //Causes the specified flags to be off.

See ios_base::setf.

2.4 ios_base::precision

streamsize precision() const; 

streamsize precision(streamsize _Prec);

Specifies the number of digits to display in a floating-point number.

The number of significant digits to display, or the number of digits after the decimal point in fixed notation.

int main( )

{

     float i = 31.31234F;

     cout.precision( 3 );

     cout << i << endl; // display three significant digits. (31.3)

     cout << fixed << i << endl; // display three digits after decimal point. (31.312)

}

 

 

posted on 2009-07-15 14:35  珍宝老王  阅读(520)  评论(0编辑  收藏  举报