[2]初始输入输出之基类ios_base

2017-01-03 19:13:48来源:CSDN作者:Soldiers5人点击



基类ios_base 定义了与字符类别和相应字符特性无关的属性(包含状态和格式标志等组件和函数)。

class _CRTIMP2_PURE ios_base : public _Iosb<int> { // base class for iospublic: _BITMASK(_Fmtflags, fmtflags);            //格式控制信息 _BITMASK(_Iostate, iostate);                           //流的状态 _BITMASK(_Openmode, openmode);            //流的打开方式 _BITMASK(_Seekdir, seekdir);            //流的定位位置 typedef _STD streamoff streamoff; typedef _STD streampos streampos; enum event  { // constants for ios events  erase_event, imbue_event, copyfmt_event};          //事件类型(擦除事件、改变locale设置事件、复制格式事件) typedef void (__CLRCALL_OR_CDECL *event_callback)(event, ios_base&, int);   //事件回调  描述传递到 register_callback函数。 typedef unsigned int io_state, open_mode, seek_dir;

_________________________________________________________________________________________________________________________________
 

enum _Fmtflags  { // constants for formatting options  _Fmtmask = 0xffff, _Fmtzero = 0}; static const _Fmtflags skipws = (_Fmtflags)_IOSskipws;                 //用于在某些提取前跳过前导空格。  static const _Fmtflags unitbuf = (_Fmtflags)_IOSunitbuf;               //用于在每次插入后刷新输出。  static const _Fmtflags uppercase = (_Fmtflags)_IOSuppercase;           //用于在某些插入中插入小写字母的大写等效项。  static const _Fmtflags showbase = (_Fmtflags)_IOSshowbase;             //用于插入一个显示已生成整型域的基的前缀。  static const _Fmtflags showpoint = (_Fmtflags)_IOSshowpoint;           //用于在生成的浮点字段中无条件地插入十进制点。  static const _Fmtflags showpos = (_Fmtflags)_IOSshowpos;               //用于在生成的非负数字段中插入一个加号。  static const _Fmtflags left = (_Fmtflags)_IOSleft;                     //用于通过在已生成字段的末尾插入填充字符(左对齐),来根据需要填充字段宽度。  static const _Fmtflags right = (_Fmtflags)_IOSright;                   //用于通过在已生成字段的开头插入填充字符(右对齐),来根据需要填充字段宽度。  static const _Fmtflags internal = (_Fmtflags)_IOSinternal;             //用于通过在生成的数字字段内的某一点插入填充字符,来根据需要填充字段宽度。 static const _Fmtflags dec = (_Fmtflags)_IOSdec;                       //用于以十进制格式插入或提取整数值。  static const _Fmtflags oct = (_Fmtflags)_IOSoct;                       //用于以八进制格式插入或提取整数值。  static const _Fmtflags hex = (_Fmtflags)_IOShex;                       //用于以十六进制格式插入或提取整数值。  static const _Fmtflags scientific = (_Fmtflags)_IOSscientific;         //用于以科学记数格式(不带指数域)插入浮点值。  static const _Fmtflags fixed = (_Fmtflags)_IOSfixed;                   //用于以固定点格式(不带指数字段)插入浮点值。  static const _Fmtflags hexfloat =  (_Fmtflags)_IOShexfloat; // added with TR1/ static const _Fmtflags boolalpha = (_Fmtflags)_IOSboolalpha;        //用于将类型 bool 的对象作为名称(如 true 和 false)而不是数值进行插入或提取。 static const _Fmtflags _Stdio = (_Fmtflags)_IOS_Stdio; static const _Fmtflags adjustfield = (_Fmtflags)(_IOSleft              //(定义为 internal 的位掩码) | left | right  | _IOSright | _IOSinternal); static const _Fmtflags basefield = (_Fmtflags)(_IOSdec                 //(定义为 dec) | hex | oct  | _IOSoct | _IOShex); static const _Fmtflags floatfield = (_Fmtflags)(_IOSscientific         //(定义为 fixed) | scientific  | _IOSfixed);  #define _IOSskipws  0x0001 #define _IOSunitbuf  0x0002 #define _IOSuppercase 0x0004 #define _IOSshowbase 0x0008 #define _IOSshowpoint 0x0010 #define _IOSshowpos  0x0020 #define _IOSleft  0x0040 #define _IOSright  0x0080 #define _IOSinternal 0x0100 #define _IOSdec   0x0200 #define _IOSoct   0x0400 #define _IOShex   0x0800 #define _IOSscientific 0x1000 #define _IOSfixed  0x2000 #define _IOShexfloat 0x3000 // added with TR1/ #define _IOSboolalpha 0x4000 #define _IOS_Stdio  0x8000 #define _IOS_Nocreate 0x40 #define _IOS_Noreplace 0x80 #define _IOSbinary  0x20 enum _Iostate  { // constants for stream states  _Statmask = 0x17}; static const _Iostate goodbit = (_Iostate)0x0;                         //流状态完全正常 static const _Iostate eofbit = (_Iostate)0x1;                          //已经到达文件尾  static const _Iostate failbit = (_Iostate)0x2;                         //输入(输出)流出现非致命错误,可挽回 static const _Iostate badbit = (_Iostate)0x4;                          //输入(输出)流出现致命错误,不可挽回 static const _Iostate _Hardfail = (_Iostate)0x10;                      //输入(输出)流出现致命错误,流不可恢复 enum _Openmode  { // constants for file opening options  _Openmask = 0xff}; static const _Openmode in = (_Openmode)0x01;                           //打开文件,已便读取 static const _Openmode out = (_Openmode)0x02;                          //打开文件,已便写入 static const _Openmode ate = (_Openmode)0x04;                          //打开文件,移到文件尾            static const _Openmode app = (_Openmode)0x08;                          //追加到文件尾 static const _Openmode trunc = (_Openmode)0x10;         //如果文件存在,则截断文件 static const _Openmode _Nocreate = (_Openmode)_IOS_Nocreate; static const _Openmode _Noreplace = (_Openmode)_IOS_Noreplace; static const _Openmode binary = (_Openmode)_IOSbinary;                 //二进制       enum _Seekdir  { // constants for file positioning options  _Seekmask = 0x3}; static const _Seekdir beg = (_Seekdir)0;                               //文件开头 static const _Seekdir cur = (_Seekdir)1;                               //文件当前位置 static const _Seekdir end = (_Seekdir)2;                               //文件结尾


_________________________________________________________________________________________________________________________________


ios_base成员函数:

fmtflags __CLR_OR_THIS_CALL flags() const        //返回当前格式控制信息fmtflags __CLR_OR_THIS_CALL flags(fmtflags _Newfmtflags)       //设置格式控制信息locale __CLR_OR_THIS_CALL getloc() const           //返回存储的区域设置对象。  locale __CLR_OR_THIS_CALL imbue(const locale& _Loc)                  //更改区域设置。                  long& __CLR_OR_THIS_CALL iword(int _Idx)                              //分配将存储为iword的值。streamsize __CLR_OR_THIS_CALL precision() const                      //指定浮点数中显示的位数。streamsize __CLR_OR_THIS_CALL precision(streamsize _Newprecision)    //指定浮点数中显示的位数。void __CLR_OR_THIS_CALL register_callback(event_callback _Pfn,int _Idx) //指定一个回调函数。fmtflags __CLR_OR_THIS_CALL setf(fmtflags _Newfmtflags)                 //设置指定标志。fmtflags __CLR_OR_THIS_CALL setf(fmtflags _Newfmtflags, fmtflags _Mask) //设置指定标志。 static bool __CLRCALL_OR_CDECL sync_with_stdio(bool _Newsync = true)    //确保 iostream 和 C 运行时库操作按照它们在源代码中出现的顺序发生。void __CLR_OR_THIS_CALL unsetf(fmtflags _Mask)                          //将导致指定的标志处于关闭状态。streamsize __CLR_OR_THIS_CALL width() const                             //返回输出流的长度streamsize __CLR_OR_THIS_CALL width(streamsize _Newwidth)               //设置输出流的长度。static int __CLRCALL_OR_CDECL xalloc()                                  //指定变量应为流的一部分。


例1:ios_base函数使用
注:例子中的每个函数最好单独测试,可能会相互影响

#include "iostream"using namespace std;int main(){ //1.flags cout << cout.flags() << endl;    //513 cout.flags(ios::hex); cout << cout.flags() << endl;    //800 (0x0800) //2.imbue,getloc cout.imbue(locale("french_france")); cout << cout.getloc().c_str() << endl;   //French_France.1252 //3.xalloc,iword,pword static const int i = ios_base::xalloc(); static const int j = ios_base::xalloc(); cout.iword(i) = 11; cin.iword(i) = 13; cin.pword(j) = "test"; cout << cout.iword(i) << endl;     //b cout << cin.iword(i) << endl;     //d cout << (char*)cin.pword(j) << endl;      //test //4.precision float f = 31.31234f; cout.precision(3); cout << f << endl;         //31.3 cout << fixed << f << endl;       //31.312 //5.setf,unsetf int i = 10; cout << i << endl;                         //10 cout.unsetf(ios_base::dec); cout.setf(ios_base::hex); cout << i << endl;                         //a cout.setf(ios_base::dec); cout << i << endl;                         //10 cout.setf(ios_base::hex, ios_base::dec);   cout << i << endl;                         //a //6.width cout.width(20); cout << cout.width() << endl;          //                  20 cout << cout.width() << endl;          //0  return 0;}


例2:event_callback使用

#include "iostream"using namespace std;void callback1(ios_base::event e,ios_base& stream,int arg){ cout << "in callback1" << endl; switch (e) { case ios_base::erase_event:  cout << "callback1 :an erase event"<<endl;  break; case ios_base::imbue_event:  cout << "callback1 :an imbue event" << endl;  break; case ios_base::copyfmt_event:  cout << "callback1 :an copyfmt event" << endl;  break; }}void callback2(ios_base::event e, ios_base& stream, int arg){ cout << "in callback2" << endl; switch (e) { case ios_base::erase_event:  cout << "callback2 :an erase event" << endl;  break; case ios_base::imbue_event:  cout << "callback2 :an imbue event" << endl;  break; case ios_base::copyfmt_event:  cout << "callback2 :an copyfmt event" << endl;  break; }}int main(){ cout.register_callback(callback1, 0);      //注册回调函数 cin.register_callback(callback2,0);  cout.imbue(locale("german"));              //这里会触发cout的imbue_event  cout.copyfmt(cin);                         //这里首先会触发cout的erase_event,接着触发cin的copyfmt_event /* 输出: in callback1 callback1 :an imbue event in callback1 callback1 :an erase event in callback2 callback2 :an copyfmt event */ return 0;}

_________________________________________________________________________________________________________________________________

类 failure 继承 system_error,定义了由函数引起的异常,在流缓冲区操作期间检测到的错误。

 // CLASS failure class failure  : public system_error  { // base of all iostreams exceptions public:  explicit failure(const string &_Message,                                //这个构造函数有两个参数, 函数what()的返回值会拷贝到_Message,   const error_code& _Errcode = make_error_code(io_errc::stream))      //第二个参数_Errcode 默认为 make_error_code   : system_error(_Errcode, _Message)   { // construct with message   }  explicit failure(const char *_Message,   const error_code& _Errcode = make_error_code(io_errc::stream))   : system_error(_Errcode, _Message)   { // construct with message   }


相关函数:   

bool __CLR_OR_THIS_CALL good() const                            //指示流处于良好状态。bool __CLR_OR_THIS_CALL eof() const                             //指示是否已到达流的结尾。bool __CLR_OR_THIS_CALL fail() const                            //指示从流中提取有效字段失败。bool __CLR_OR_THIS_CALL bad() const                             //指示流缓冲区的完整性损失。iostate __CLR_OR_THIS_CALL exceptions() const                   //指示流将引发哪些异常。void __CLR_OR_THIS_CALL exceptions(iostate _Newexcept)void __CLR_OR_THIS_CALL exceptions(io_state _State)


例1:错误和异常

#include "iostream"using namespace std;int main(){ //1.bad good fail bad bool b = cout.bad();  cout << boolalpha;     //指定输出格式为bool类型 cout << b << endl;     //false b = cout.good(); cout << b << endl;     //true; //2.exceptions cout << hex;      //指定输出格式为16进制 cout << cout.exceptions() << endl;; //0 (goodbit) cout.exceptions(ios::eofbit);         cout <<cout.exceptions() << endl;   //1 (eofbit) try {  cout.clear(ios::eofbit);     //清除eofbit } catch (exception &e) {  cout.clear();  cout << typeid(e).name() << endl;  //class std::ios_base::failure    cout << e.what() << endl;          //ios_base::eofbit set:iostream stream error }}




最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台