敬业的IT人 >> 编程开发 >> C/C++ >> C++的iostream标准库介绍

C++的iostream标准库介绍

敬业的IT人 互联网 佚名 2008-1-3 19:17:22
引用:

  我们从一开始就一直在利用C++的输入输出在做着各种练习,输入输出是由iostream库提供的,所以讨论此标准库是有必要的,它与C语言的stdio库不同,它从一开始就是用多重继承与虚拟继承实现的面向对象的层次结构,作为一个c++的标准库组件提供给程序员使用。

  iostream为内置类型类型对象提供了输入输出支持,同时也支持文件的输入输出,类的设计者可以通过对iostream库的扩展,来支持自定义类型的输入输出操作。

  为什么说要扩展才能提供支持呢?我们来一个示例。

#include<stdio.h> 
#include<iostream> 
usingnamespacestd;   
 
classTest 
{ 
   public: 
     Test(inta=0,intb=0) 
     { 
       Test::a=a; 
       Test::b=b; 
     } 
   inta; 
   intb; 
}; 
intmain() 
{ 
   Testt(100,50); 
   printf("%???",t);//不明确的输出格式 
   scanf("%???",t);//不明确的输入格式 
   cout<<t<<endl;//同样不够明确 
   cin>>t;//同样不够明确 
   system("pause"); 
}

  由于自定义类的特殊性,在上面的代码中,无论你使用c风格的输入输出,或者是c++的输入输出都不是不明确的一个表示,由于c语言没有运算符重载机制,导致stdio库的不可扩充性,让我们无法让printf()和scanf()支持对自定义类对象的扩充识别,而c++是可以通过运算符重载机制扩充iostream库的,使系统能能够识别自定义类型,从而让输入输出明确的知道他们该干什么,格式是什么。

  在上例中我们之所以用printf与cout进行对比目的是为了告诉大家,C与C++处理输入输出的根本不同,我们从c远的输入输出可以很明显看出是函数调用方式,而c++的则是对象模式,cout和cin是ostream类和istream类的对象。

  C++中的iostream库主要包含下图所示的几个头文件:

  C++的iostream标准库介绍(图一)

  • 如何重装xp系统图解
  • 下载Flash播放插件
  • 巧妙清除Windows 2000/XP登录密码
  • 如何利用路由器设置局域网
  • QQ空间打不开
  • 开机后鼠标不动怎么办
  • Excel密码保护的解除方法与解除原理
  • Windows XP注册表详解
  • 3dmax不锈钢金属材质的制作方法
  • 硬盘变成raw格式怎么办

  iostream库定义了以下三个标准流对象:

  1.cin,表示标准输入(standard input)的istream类对象。cin使我们可以从设备读如数据。

  2.cout,表示标准输出(standard output)的ostream类对象。cout使我们可以向设备输出或者写数据。

  3.cerr,表示标准错误(standard error)的osttream类对象。cerr是导出程序错误消息的地方,它只能允许向屏幕设备写数据。

  输出主要由重载的左移操作符(<<)来完成,输入主要由重载的右移操作符(>>)完成。

  >>a表示将数据放入a对象中。

  <<a表示将a对象中存储的数据拿出。

  这些标准的流对象都有默认的所对应的设备,见下表:

  C++的iostream标准库介绍(图三)

  图中的意思表明cin对象的默认输入设备是键盘,cout对象的默认输出设备是显示器屏幕。

  那么原理上C++有是如何利用cin/cout对象与左移和右移运算符重载来实现输入输出的呢?

  下面我们以输出为例,说明其实现原理:

  cout是ostream类的对象,因为它所指向的是标准设备(显示器屏幕),所以它在iostream头文件中作为全局对象进行定义。

  由于文件设备并不像显示器屏幕与键盘那样是标准默认设备,所以它在fstream.h头文件中是没有像cout那样预先定义的全局对象,所以我们必须自己定义一个该类的对象,我们要以文件作为设备向文件输出信息(也就是向文件写数据),那么就应该使用ofstream类。

  ofstream类的默认构造函数原形为:

  ofstream::ofstream(cborder="0" alt="C++的iostream标准库介绍(图五)" width="383" height="203" />

  • 如何重装xp系统图解
  • 下载Flash播放插件
  • 巧妙清除Windows 2000/XP登录密码
  • 如何利用路由器设置局域网
  • QQ空间打不开
  • 开机后鼠标不动怎么办
  • Excel密码保护的解除方法与解除原理
  • Windows XP注册表详解
  • 3dmax不锈钢金属材质的制作方法
  • 硬盘变成raw格式怎么办

  串流同样不是标准设备,不会有预先定义好的全局对象,所以不能直接操作,需要通过构造函数创建对象。

  类istrstream的构造函数原形如下:

  istrstream::istrstream(cborder="0" alt="C++的iostream标准库介绍(图六)" width="383" height="203" />

  istringstream是由一个string对象构造而来,istringstream类从一个string对象读取字符。

  istringstream的构造函数原形如下:

  istringstream::istringstream(string str);

文章地址: 进入讨论组讨论。 引用://程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
#include<iostream> 
#include<sstream> 
usingnamespacestd; 
intmain() 
{ 
istringstreamistr; 
istr.str("156.7",); 
//上述两个过程可以简单写成istringstreamistr("156.7"); 
cout<<istr.str()<<endl; 
inta; 
floatb; 
istr>>a; 
cout<<a<<endl; 
istr>>b; 
cout<<b<<endl; 
system("pause"); 
}

  上例中,构造字符串流的时候,空格会成为字符串参数的内部分界,例子中对a,b对象的输入"赋值"操作证明了这一点,字符串的空格成为了整型数据与浮点型数据的分解点,利用分界获取的方法我们事实上完成了字符串到整型对象与浮点型对象的拆分转换过程。

  str()成员函数的使用可以让istringstream对象返回一个string字符串(例如本例中的输出操作(cout<<istr.str();)。

  ostringstream同样是由一个string对象构造而来,ostringstream类向一个string插入字符。

  ostringstream的构造函数原形如下:

  ostringstream::ostringstream(string str);

  示例代码如下:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
#include<iostream> 
#include<sstream> 
#include<string> 
usingnamespacestd; 
intmain() 
{ 
ostringstreamostr; 
//ostr.str("abc");//如果构造的时候设置了字符串参数,那么增长操作的时候不会从结尾开始增加,而是修改原有数据,超出的部分增长 
ostr.put('d'); 
ostr.put('e'); 
ostr<<"fg"; 
 
stringgstr=ostr.str(); 
cout<<gstr; 
system("pause"); 
}

  在上例代码中,我们通过put()或者左移操作符可以不断向ostr插入单个字符或者是字符串,通过str()函数返回增长过后的完整字符串数据,但值得注意的一点是,当构造的时候对象内已经存在字符串数据的时候,那么增长操作的时候不会从结尾开始增加,而是修改原有数据,超出的部分增长。

进入讨论组讨论。

  对于stringstream了来说,不用我多说,大家也已经知道它是用于C++风格的字符串的输入输出的。

  stringstream的构造函数原形如下:

  stringstream::stringstream(string str);

  示例代码如下:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
#include<iostream> 
#include<sstream> 
#include<string> 
usingnamespacestd; 
 
intmain() 
{ 
   stringstreamostr("ccc"); 
   ostr.put('d'); 
   ostr.put('e'); 
   ostr<<"fg"; 
   stringgstr=ostr.str(); 
   cout<<gstr<<endl; 
 
   chara; 
   ostr>>a; 
   cout<<a 
   
   system("pause"); 
}

  除此而外,stringstream类的对象我们还常用它进行string与各种内置类型数据之间的转换。

  示例代码如下:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
#include<iostream> 
#include<sstream> 
#include<string> 
usingnamespacestd; 
 
intmain() 
{ 
   stringstreamsstr; 
//--------int转string----------- 
   inta=100; 
   stringstr; 
   sstr<<a; 
   sstr>>str; 
   cout<<str<<endl; 
//--------string转char[]-------- 
   sstr.clear();//如果你想通过使用同一stringstream对象实现多种类型的转换,请注意在每一次转换之后都必须调用clear()成员函数。 
   stringname="colinguan"; 
   charcname[200]; 
   sstr<<name; 
   sstr>>cname; 
   cout<<cname; 
   system("pause"); 
}

  接下来我们来学习一下输入/输出的状态标志的相关知识,C++中负责的输入/输出的系统包括了关于每一个输入/输出操作的结果的记录信息。这些当前的状态信息被包含在io_state类型的对象中。io_state是一个枚举类型(就像open_mode一样),以下便是它包含的值。

文章地址: 进入讨论组讨论。

  goodbit 无错误

  Eofbit 已到达文件尾

  failbit 非致命的输入/输出错误,可挽回

  badbit 致命的输入/输出错误,无法挽回

  有两种方法可以获得输入/输出的状态信息。一种方法是通过调用rdstate()函数,它将返回当前状态的错误标记。例如,假如没有任何错误,则rdstate()会返回goodbit.

  下例示例,表示出了rdstate()的用法:

//程序作者:管宁  
//站点:www.cndev-lab.com  
//所有稿件均有版权,如要转载,请务必著名出处和作者  
 
#include<iostream> 
usingnamespacestd; 
 
intmain() 
{ 
   inta; 
   cin>>a; 
   cout<<cin.rdstate()<<endl; 
   if(cin.rdstate()==ios::goodbit) 
   { 
     cout<<"输入数据的类型正确,无错误!"<<endl; 
   } 
   if(cin.rdstate()==ios_base::failbit) 
   { 
     cout<<"输入数据类型错误,非致命错误,可清除输入缓冲区挽回!"<<endl; 
   } 
   system("pause"); 
}

  另一种方法则是使用下面任何一个函数来检测相应的输入/输出状态:

bool bad();
bool eof();
bool fail();
bool good();

  下例示例,表示出了上面各成员函数的用法:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
 
#include<iostream> 
usingnamespacestd; 
 
intmain() 
{ 
   inta; 
   cin>>a; 
   cout<<cin.rdstate()<<endl; 
   if(cin.good()) 
   { 
     cout<<"输入数据的类型正确,无错误!"<<endl; 
   } 
   if(cin.fail()) 
   { 
     cout<<"输入数据类型错误,非致命错误,可清除输入缓冲区挽回!"<<endl; 
   } 
   system("pause"); 
}

  如果错误发生,那么流状态既被标记为错误,你必须清除这些错误状态,以使你的程序能正确适当地继续运行。要清除错误状态,需使用clear()函数。此函数带一个参数,它是你将要设为当前状态的标志值。,只要将ios::goodbit作为实参。

进入讨论组讨论。

  示例代码如下:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
 
#include<iostream> 
usingnamespacestd; 
 
intmain() 
{ 
   inta; 
   cin>>a; 
   cout<<cin.rdstate()<<endl; 
   cin.clear(ios::goodbit); 
   cout<<cin.rdstate()<<endl; 
   system("pause"); 
}

  通常当我们发现输入有错又需要改正的时候,使用clear()更改标记为正确后,同时也需要使用get()成员函数清除输入缓冲区,以达到重复输入的目的。

  示例代码如下:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
#include<iostream> 
usingnamespacestd; 
 
intmain() 
{ 
   inta; 
   while(1) 
   { 
     cin>>a; 
     if(!cin)//条件可改写为cin.fail() 
     { 
       cout<<"输入有错!请重新输入"<<endl; 
       cin.clear(); 
       cin.get(); 
     } 
     else 
     { 
       cout<<a; 
       break; 
     } 
   } 
   system("pause"); 
}

  最后再给出一个对文件流错误标记处理的例子,巩固学习,代码如下:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
#include<iostream> 
#include<fstream> 
usingnamespacestd; 
 
intmain() 
{ 
   ifstreammyfile("c:1.txt",ios_base::in,0); 
   if(myfile.fail()) 
   { 
     cout<<"文件读取失败或指定文件不存在!"<<endl; 
   } 
   else 
   { 
     charch; 
     while(myfile.get(ch)) 
     { 
       cout<<ch; 
     } 
     if(myfile.eof()) 
     { 
       cout<<"文件内容已经全部读完"<<endl; 
     } 
     while(myfile.get(ch)) 
     { 
       cout<<ch; 
     } 
   } 
   system("pause"); 
}

  C语言提供了格式化输入输出的方法,C++也同样,但是C++的控制符使用起来更为简单方便,在c++下有两中方法控制格式化输入输出。

进入讨论组讨论。

  1.有流对象的成员函数。

  例如,下列程序以成员函数的方式控制输出的精度:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
 
#include<iostream> 
usingnamespacestd;  
intmain()  
{  
   floatpi=3.14159f; 
   cout<<pi<<endl; 
   cout.precision(2); 
   cout<<pi<<endl; 
   system("pause");  
}

  2.使用C++输入输出控制符,控制符是在拖文件iomanip.h中定义的对象,与成员函数有一样的效果,控制符不必像成员函数学那样单独调用,它可以直接插入流中使用。

  例如,下列程序以控制符的方式控制输出的精度:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
 
#include<iostream> 
#include<iomanip> 
usingnamespacestd;  
intmain()  
{  
   floatpi=3.14159f; 
   cout<<pi<<endl; 
   cout<<setprecision(4); 
   cout<<pi<<endl; 
   system("pause");  
}

  下表我们列出了一些比较常用的控制符号,由于篇幅有限读者请根据自己的需要查阅相关书籍:

  对于iostream标准库来说包含了众多的成员函数,各函数都有其自身的作用,篇幅问题笔者在这里不能一一说明例举,由于标准输入对象cin提供输入的时候会自动以空格作为分界,给我们获取一行带有空格的完整字符串带来了困难,在这里补充一个非常用有的成员函数----getline()。

  其函数原型为:

  getlin(chiar *str,int size,char=' ');

  第一个参数是字符数组,用于存放整行文本,第二个参数读取的最大字符个数,第三个参数为作为分界界限的字符,默认识是,换行符。

  示例代码如下:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
 
#include<iostream> 
#include<iomanip> 
usingnamespacestd;  
intmain()  
{  
   charstr[100]; 
   cin.getline(str,sizeof(str),' '); 
   cout<<str<<endl; 
   system("pause");  
}

  通过上面内容的学习,我们对i/o有了一些基本点基本的认识,现在是该切入正题的时候了,详细学习一下,如何重载左移与右移操作符。

文章地址: 进入讨论组讨论。

  先说左移(<<)操作符,也就是我们常说的输出操作符。

  对于自定义类来说,重载左移操作符的方法我们常使用类的友元方式进行操作。

  示例代码如下:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
 
#include<iostream> 
usingnamespacestd; 
 
classTest 
{ 
   public: 
     Test(intage=0,char*name="") 
     { 
       Test::age=age; 
       strcpy(Test::name,name); 
     } 
     voidoutmembers(ostream&out) 
     { 
       out<<"Age:"<<age<<endl<<"Name:"<<this->name<<endl; 
     } 
     friendostream&operator<<(ostream&,Test&); 
   protected: 
     intage; 
     charname[50]; 
}; 
ostream&operator<<(ostream&out,Test&temp) 
{ 
   temp.outmembers(out); 
   returnout; 
} 
intmain()  
{ 
   Testa(24,"管宁"); 
   cout<<a; 
   system("pause"); 
}

  上例代码中,我们对void outmembers(ostream &out)的参数使用ostream定义主要是为了可以向它传递任何ostream类对象不光是cout也可以是ofstrem或者是ostrstream和ostringstream类对象,做到通用性。

  重载运算符,我们知道可以是非成员方式也可以是成员方式的,对于<<来说同样也可以是成员方式,但我十分不推荐这么做,因为对于类的成员函数来说,第一个参数始终是会被隐藏的,而且一定是当前类类型的。

  下面的示例代码就是将上面的<<重载函数修改成成员方式的做法:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
 
#include<iostream> 
usingnamespacestd; 
 
classTest 
{ 
   public: 
     Test(intage=0,char*name="") 
     { 
       Test::age=age; 
       strcpy(Test::name,name); 
     } 
     voidoutmembers(ostream&out) 
     { 
       out<<"Age:"<<age<<endl<<"Name:"<<this->name<<endl; 
     } 
     ostream&operator<<(ostream&out) 
     { 
       this->outmembers(out); 
       returnout; 
     } 
   protected: 
     intage; 
     charname[50]; 
}; 
intmain()  
{ 
   Testa(24,"管宁"); 
   a<<cout; 
   system("pause"); 
}

  从代码实现上,我们将函数修改成了ostream& operator <<(ostream &out),迫不得已将ostream类型的引用参数放到了后面,这是因为,成员方式运算符重载函数第一个参数会被隐藏,而且一定是当前类类型的,这和ostream类型冲突了。由此我们在使用cout输出的时候就必须写成a<<cout;,这样一来代码的可读行就大大降低了,这到底是左移还是右移呢?为此我再一次说明,对于左移和右移运算符的重载是十分不推荐使用成员函数的方式编写的。

进入讨论组讨论。

  为了巩固学习,下面我们以fstream对象输出为例做一个练习。

  代码如下:

//程序作者:管宁 
//站点:www.cndev-lab.com 
//所有稿件均有版权,如要转载,请务必著名出处和作者 
#include<iostream> 
#include<fstream> 
usingnamespacestd;  
classTest  
{  
   public:  
     Test(intage=0,char*name="")  
     {  
       Test::age=age;  
       strcpy(Test::name,name);  
     }  
     voidoutmembers(ostream&out)  
     {  
       out<<"Age:"<<age<<endl<<"Name:"<<this->name<<endl;  
     }  
     friendostream&operator<<(ostream&,Test&);  
   protected:  
     intage;  
     charname[50];  
};  
ostream&operator<<(ostream&out,Test&temp)  
{  
   temp.outmembers(out);  
   returnout;  
}  
intmain()   
{  
   Testa(24,"管宁"); 
   ofstreammyfile("c:1.txt",ios::out,0); 
   if(myfile.rdstate()==ios_base::goodbit) 
   { 
     myfile<<a; 
     cout<<"文件创建成功,写入正常!"<<endl; 
   } 
   if(myfile.rdstate()==ios_base::badbit) 
   { 
     cout<<"文件创建失败,磁盘错误!"<<endl; 
   } 
   system("pause"); 
}

  对于左移运算符重载函数来说,由于不推荐使用成员方式,那么使用非成员方式在类有多重继承的情况下,就不能使用虚函数进行左移运算符重载的区分,为了达到能够区分显示的目的,给每个类分别添加不同的虚函数是必要的。

  示例代码如下:

//程序作者:管宁  
//站点:www.cndev-lab.com  
//所有稿件均有版权,如要转载,请务必著名出处和作者  
 
#include<iostream> 
#include<fstream> 
usingnamespacestd;  
classStudent  
{  
   public:  
     Student(intage=0,char*name="")  
     {  
       Student::age=age;  
       strcpy(Student::name,name);  
     }  
     virtualvoidoutmembers(ostream&out)=0; 
     friendostream&operator<<(ostream&,Student&);  
   protected:  
     intage;  
     charname[50];  
};  
ostream&operator<<(ostream&out,Student&temp)  
{ 
   temp.outmembers(out);  
   returnout;  
} 
classAcademician:publicStudent 
{ 
   public: 
     Academician(intage=0,char*name="",char*speciality=""):Student(age,name) 
     { 
       strcpy(Academician::speciality,speciality); 
     } 
     virtualvoidoutmembers(ostream&out)  
     {  
       out<<"Age:"<<age<<endl<<"Name:"<<name<<endl<<
       "speciality:"<<speciality<<endl;  
     } 
   protected: 
     charspeciality[80]; 
}; 
classGraduateStudent:publicAcademician 
{ 
   public: 
     GraduateStudent(intage=0,char*name="",char*speciality="",
     char*investigate=""):Academician(age,name,speciality) 
     { 
       strcpy(GraduateStudent::investigate,investigate); 
     } 
     virtualvoidoutmembers(ostream&out)  
     {  
       out<<"Age:"<<age<<endl<<"Name:"<<name<<endl<<
       "speciality:"<<speciality<<endl<<"investigate:"<<investigate<<endl;  
     } 
   protected: 
     charinvestigate[100]; 
}; 
intmain() 
{  
   Academiciana(24,"管宁","ComputerScience"); 
   cout<<a; 
   GraduateStudentb(24,"严燕玲","ComputerScience","GISSystem"); 
   cout<<b; 
   system("pause"); 
}

  在上面的代码中为了能够区分输出a对象与b对象,我们用虚函数的方式重载了继承类Academician与多重继承类GraduateStudent的outmembers成员函数,由于ostream& operator <<(ostream& out,Student &temp) 运算符重载函数是Student基类的,Student &temp参数通过虚函数的定义可以适应不同派生类对象,所以在其内部调用temp.outmembers(out); 系统可识别不同继类的outmembers()成员函数。

进入讨论组讨论。

  最后看一下,右移运算符的重载,右移运算符我们也常叫它输入运算符号,对于它来说,具体实现和左移运算符的重载差别并不大,对于有多成员对象的类来说,只要保证能够完整输入各成员对象大数据就可以了。

  示例如下:

//程序作者:管宁  
//站点:www.cndev-lab.com  
//所有稿件均有版权,如要转载,请务必著名出处和作者  
#include<iostream>  
usingnamespacestd;  
classTest  
{  
   public:  
     Test(intage=0,char*name="")  
     {  
       Test::age=age;  
       strcpy(Test::name,name);  
     }  
     voidinputmembers(istream&out)  
     { 
       cout<<"pleaseinputage:"; 
       cin>>Test::age; 
       cout<<"pleaseinputname:"; 
       cin>>Test::name; 
     }  
     friendistream&operator>>(istream&,Test&);  
   public:  
     intage;  
     charname[50];  
};  
istream&operator>>(istream&input,Test&temp)  
{  
   temp.inputmembers(input);  
   returninput;  
}  
intmain()   
{  
   Testa;  
   cin>>a; 
   cout<<a.age<<"|"<<a.name<<endl; 
   system("pause");  
}

正文:进入讨论组讨论。
粤ICP备06119539号
Copyright CiscoSky.Org,Some Rights Reserved.
Email:me1228#tom.com