📄 c++的iostream标准库介绍.htm
字号:
Test t(100,50);
printf("<FONT color=blue>%???</FONT>",t);<FONT color=green>//不明确的输出格式 </FONT>
scanf("<FONT color=blue>%???</FONT>",t);<FONT color=green>//不明确的输入格式 </FONT>
cout<<t<<endl;<FONT color=green>//同样不够明确 </FONT>
cin>>t;<FONT color=green>//同样不够明确 </FONT>
system("<FONT color=blue>pause</FONT>");
}</PRE><PRE></PRE></DIV> 由于自定义类的特殊性,在上面的代码中,无论你使用c风格的输入输出,或者是c++的输入输出都不是不明确的一个表示,由于c语言没有运算符重载机制,导致stdio库的不可扩充性,让我们无法让printf()和scanf()支持对自定义类对象的扩充识别,而c++是可以通过运算符重载机制扩充
iostream库的,使系统能能够识别自定义类型,从而让输入输出明确的知道他们该干什么,格式是什么。
<P> 在上例中我们之所以用printf与cout进行对比目的是为了告诉大家,C与C++处理输入输出的根本不同,我们从c远的输入输出可以很明显看出是函数调用方式,而c++的则是对象模式,cout和cin是ostream类和istream类的对象。
<H3><A name="1 iostream: istream 和 ostream"></A>1 iostream: istream 和 ostream
</H3> C++中的iostream库主要包含下图所示的几个头文件:
<TABLE class=twikiTable cellSpacing=1 cellPadding=1 border=0>
<TBODY>
<TR>
<TH class=twikiFirstCol bgColor=#dadada colSpan=2><STRONG><FONT
color=#000000>IOSstream 库</FONT></STRONG> </TH></TR>
<TR>
<TD class=twikiFirstCol bgColor=#ffffff>fstream </TD>
<TD bgColor=#ffffff>iomainip </TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#eaeaea>ios </TD>
<TD bgColor=#eaeaea>iosfwd </TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#ffffff>iostream </TD>
<TD bgColor=#ffffff>istream </TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#eaeaea>ostream </TD>
<TD bgColor=#eaeaea>sstream </TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#ffffff>streambuf </TD>
<TD bgColor=#ffffff>strstream </TD></TR></TBODY></TABLE>
<P> 我们所熟悉的输入输出操作分别是由istream(输入流)和ostream(输出流)这两个类提供的,为了允许双向的输入/输出,由istream和ostream派生出了iostream类。
<P> 类的继承关系见下图:<BR><IMG src="C++的iostream标准库介绍.files/05cppios02.gif">
<P>iostream库定义了以下三个标准流对象:
<OL>
<LI>cin,表示标准输入(standard input)的istream类对象。cin使我们可以从设备读如数据。
<LI>cout,表示标准输出(standard output)的ostream类对象。cout使我们可以向设备输出或者写数据。
<LI>cerr,表示标准错误(standard error)的osttream类对象。cerr是导出程序错误消息的地方,它只能允许向屏幕设备写数据。
</LI></OL>
<P> 输出主要由重载的左移操作符(<<)来完成,输入主要由重载的右移操作符(>>)完成:
<OL>
<LI>>>a表示将数据放入a对象中。
<LI><<a表示将a对象中存储的数据拿出。 </LI></OL>
<P> 这些标准的流对象都有默认的所对应的设备,见下表:<BR>
<TABLE class=twikiTable cellSpacing=1 cellPadding=1 border=0>
<TBODY>
<TR>
<TH class=twikiFirstCol bgColor=#dadada><A title="Sort by this column"
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?sortcol=0;table=2;up=0#sorted_table"
rel=nofollow><FONT color=#000000>C++对象名</FONT></A> </TH>
<TH bgColor=#dadada><A title="Sort by this column"
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?sortcol=1;table=2;up=0#sorted_table"
rel=nofollow><FONT color=#000000>设备名称</FONT></A> </TH>
<TH bgColor=#dadada><A title="Sort by this column"
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?sortcol=2;table=2;up=0#sorted_table"
rel=nofollow><FONT color=#000000>C中标准设备名</FONT></A> </TH>
<TH bgColor=#dadada><A title="Sort by this column"
href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?sortcol=3;table=2;up=0#sorted_table"
rel=nofollow><FONT color=#000000>默认含义</FONT></A> </TH></TR>
<TR>
<TD class=twikiFirstCol align=middle bgColor=#ffffff>cin </TD>
<TD align=middle bgColor=#ffffff>键盘 </TD>
<TD align=middle bgColor=#ffffff>stdin </TD>
<TD align=middle bgColor=#ffffff>标准输入 </TD></TR>
<TR>
<TD class=twikiFirstCol align=middle bgColor=#eaeaea>cout </TD>
<TD align=middle bgColor=#eaeaea>显示器屏幕 </TD>
<TD align=middle bgColor=#eaeaea>stdout </TD>
<TD align=middle bgColor=#eaeaea>标准输出 </TD></TR>
<TR>
<TD class=twikiFirstCol align=middle bgColor=#ffffff>cerr </TD>
<TD align=middle bgColor=#ffffff>显示器屏幕 </TD>
<TD align=middle bgColor=#ffffff>stderr </TD>
<TD align=middle bgColor=#ffffff>标准错误输出
</TD></TR></TBODY></TABLE> 上表中的意思表明cin对象的默认输入设备是键盘,cout对象的默认输出设备是显示器屏幕。
<P> 那么原理上C++有是如何利用cin/cout对象与左移和右移运算符重载来实现输入输出的呢?
<P> 下面我们以输出为例,说明其实现原理:
<OL>
<LI>cout是ostream类的对象,因为它所指向的是标准设备(显示器屏幕),所以它在iostream头文件中作为全局对象进行定义。
<LI>ostream cout(stdout);//其默认指向的C中的标准设备名,作为其构造函数的参数使用。
<LI>在iostream.h头文件中,ostream类对应每个基本数据类型都有其友元函数对左移操作符进行了友元函数的重载。
<UL>
<LI>ostream& operator<<(ostream &temp,int source);
<LI>ostream& operator<<(ostream &temp,char *ps);
<LI>... 等等 </LI></UL></LI></OL>
<P> 一句输出语句:cout<<"www.cndev-lab.com";,事实上调用的就是ostream&
operator<<(ostream &temp,char
*ps);这个运算符重载函数,由于返回的是流对象的引用,引用可以作为左值使用,所以当程序中有类似cout<<"www.cndev-
lab.com"<<"中国软件开发实验室";这样的语句出现的时候,就能够构成连续输出。
<P> 由于iostream库不光支持对象的输入输出,同时也支持文件流的输入输出,所以在详细讲解左移与右移运算符重载只前,我们有必要先对文件的输入输出以及输入输出的控制符有所了解。
<H3><A name="2 fstream: ifstream 和 ofstream"></A>2 fstream: ifstream 和 ofstream
</H3> 和文件有关系的输入输出类主要在fstream.h这个头文件中被定义,在这个头文件中主要被定义了三个类,由这三个类控制对文件的各种输入输出操作,他们分别是ifstream、ofstream、fstream,其中fstream类是由iostream类派生而来,他们之间的继承关系见下图所示。<BR><IMG
src="C++的iostream标准库介绍.files/05cppios04.gif">
<P>由于文件设备并不像显示器屏幕与键盘那样是标准默认设备,所以它在fstream.h头文件中是没有像cout那样预先定义的全局对象,所以我们必须自己定义一个该类的对象,我们要以文件作为设备向文件输出信息(也就是向文件写数据),那么就应该使用ofstream类。
<P> ofstream类的默认构造函数原形为:
<DIV class=fragment><PRE> ofstream::ofstream(<FONT color=brown>const</FONT> <FONT color=brown>char</FONT> *filename,<FONT color=brown>int</FONT> mode = ios::out,<FONT color=brown>int</FONT> openprot = filebuf::openprot);</PRE><PRE></PRE></DIV>
<UL>
<LI>filename: 要打开的文件名
<LI>mode: 要打开文件的方式
<LI>prot: 打开文件的属性 </LI></UL>
<P> 其中mode和openprot这两个参数的可选项表见下表:
<TABLE class=twikiTable cellSpacing=1 cellPadding=1 border=0>
<TBODY>
<TR>
<TD class=twikiFirstCol align=middle bgColor=#eaeaea colSpan=2>mode属性表
</TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#ffffff>ios::app </TD>
<TD bgColor=#ffffff>以追加的方式打开文件 </TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#eaeaea>ios::ate </TD>
<TD bgColor=#eaeaea>文件打开后定位到文件尾,ios:app就包含有此属性 </TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#ffffff>ios::binary </TD>
<TD bgColor=#ffffff>以二进制方式打开文件,缺省的方式是文本方式。两种方式的区别见前文 </TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#eaeaea>ios::in </TD>
<TD bgColor=#eaeaea>文件以输入方式打开 </TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#ffffff>ios::out </TD>
<TD bgColor=#ffffff>文件以输出方式打开 </TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#eaeaea>ios::trunc </TD>
<TD bgColor=#eaeaea>如果文件存在,把文件长度设为0
</TD></TR></TBODY></TABLE> 可以用“或”把以上属性连接起来,如ios::out|ios::binary。
<TABLE class=twikiTable cellSpacing=1 cellPadding=1 border=0>
<TBODY>
<TR>
<TD class=twikiFirstCol align=middle bgColor=#eaeaea colSpan=2>openprot属性表
</TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#ffffff>属性 </TD>
<TD align=middle bgColor=#ffffff>含义 </TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#eaeaea>0 </TD>
<TD bgColor=#eaeaea>普通文件,打开访问 </TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#ffffff>1 </TD>
<TD bgColor=#ffffff>只读文件 </TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#eaeaea>2 </TD>
<TD bgColor=#eaeaea>隐含文件 </TD></TR>
<TR>
<TD class=twikiFirstCol bgColor=#ffffff>4 </TD>
<TD bgColor=#ffffff>系统文件 </TD></TR></TBODY></TABLE> 可以用“或”或者“+”把以上属性连接起来
,如3或1|2就是以只读和隐含属性打开文件。
<P>实例代码如下:
<DIV class=fragment><PRE><FONT color=navy>#include</FONT> <fstream>
<FONT color=brown>using</FONT> <FONT color=brown>namespace</FONT> std;
<FONT color=brown>int</FONT> main()
{
ofstream myfile("<FONT color=blue>c:\\1.txt</FONT>",ios::out|ios::trunc,0);
myfile<<"<FONT color=blue>中国软件开发实验室</FONT>"<<endl<<"<FONT color=blue>网址:</FONT>"<<"<FONT color=blue>www.cndev-lab.com</FONT>";
myfile.close()
system("<FONT color=blue>pause</FONT>");
}</PRE><PRE></PRE></DIV> 文件使用完后可以使用close成员函数关闭文件。
<P> ios::app为追加模式,在使用追加模式的时候同时进行文件状态的判断是一个比较好的习惯。
<P> 示例如下:
<P>
<DIV class=fragment><PRE><FONT color=navy>#include</FONT> <iostream>
<FONT color=navy>#include</FONT> <fstream>
<FONT color=brown>using</FONT> <FONT color=brown>namespace</FONT> std;
<FONT color=brown>int</FONT> main()
{
ofstream myfile("<FONT color=blue>c:\\1.txt</FONT>",ios::app,0);
<FONT color=brown>if</FONT>(!myfile)<FONT color=green>//或者写成myfile.fail() </FONT>
{
cout<<"<FONT color=blue>文件打开失败,目标文件状态可能为只读!</FONT>";
system("<FONT color=blue>pause</FONT>");
exit(1);
}
myfile<<"<FONT color=blue>中国软件开发实验室</FONT>"<<endl<<"<FONT color=blue>网址:</FONT>"<<"<FONT color=blue>www.cndev-lab.com</FONT>"<<endl;
myfile.close();
}</PRE><PRE></PRE></DIV>
<P> 在定义ifstream和ofstream类对象的时候,我们也可以不指定文件。以后可以通过成员函数open()显式的把一个文件连接到一个类对象上。
<P> 例如:
<P>
<DIV class=fragment><PRE><FONT color=navy>#include</FONT> <iostream>
<FONT color=navy>#include</FONT> <fstream>
<FONT color=brown>using</FONT> <FONT color=brown>namespace</FONT> std;
<FONT color=brown>int</FONT> main()
{
ofstream myfile;
myfile.open("<FONT color=blue>c:\\1.txt</FONT>",ios::out|ios::app,0);
<FONT color=brown>if</FONT>(!myfile)<FONT color=green>//或者写成myfile.fail() </FONT>
{
cout<<"<FONT color=blue>文件创建失败,磁盘不可写或者文件为只读!</FONT>";
system("<FONT color=blue>pause</FONT>");
exit(1);
}
myfile<<"<FONT color=blue>中国软件开发实验室</FONT>"<<endl<<"<FONT color=blue>网址:</FONT>"<<"<FONT color=blue>www.cndev-lab.com</FONT>"<<endl;
myfile.close();
}</PRE><PRE></PRE></DIV> 下面我们来看一下是如何利用ifstream类对象,将文件中的数据读取出来,然后再输出到标准设备中的例子。
<P> 代码如下:
<DIV class=fragment><PRE><FONT color=navy>#include</FONT> <iostream>
<FONT color=navy>#include</FONT> <fstream>
<FONT color=navy>#include</FONT> <string>
<FONT color=brown>using</FONT> <FONT color=brown>namespace</FONT> std;
<FONT color=brown>int</FONT> main()
{
ifstream myfile;
myfile.open("<FONT color=blue>c:\\1.txt</FONT>",ios::in,0);
<FONT color=brown>if</FONT>(!myfile)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -