﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-&lt;b&gt;&lt;font color=#ff0000&gt;清木@_@逍遥&lt;/font&gt;&lt;/b&gt;-随笔分类-C/C++</title><link>http://www.cppblog.com/yuqilin1228/category/7165.html</link><description>路在脚下</description><language>zh-cn</language><lastBuildDate>Fri, 26 Mar 2010 21:08:52 GMT</lastBuildDate><pubDate>Fri, 26 Mar 2010 21:08:52 GMT</pubDate><ttl>60</ttl><item><title>【C++常识】C++的iostream标准库介绍</title><link>http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110620.html</link><dc:creator>LynnRaymond</dc:creator><author>LynnRaymond</author><pubDate>Fri, 26 Mar 2010 14:03:00 GMT</pubDate><guid>http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110620.html</guid><wfw:comment>http://www.cppblog.com/yuqilin1228/comments/110620.html</wfw:comment><comments>http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110620.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuqilin1228/comments/commentRss/110620.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuqilin1228/services/trackbacks/110620.html</trackback:ping><description><![CDATA[<strong> 为什么需要iostream
我们从一开始就一直在利用C++的输入输出在做着各种练习，输入输出是由iostream库提供的，所以讨论此标准库是有必要的，它与C语言的
stdio库不同，它从一开始就是用多重继承与虚拟继承实现的面向对象的层次结构，作为一个c++的标准库组件提供给程序员使用。 </strong>
<p><strong> 　　iostream为内置类型类型对象提供了输入输出支持，同时也支持文件的输入输出，类的设计者可以通过对iostream库
的扩展，来支持自定义类型的输入输出操作。 </strong> </p>
<p><strong> 　　为什么说要扩展才能提供支持呢？我们来一个示例。 %CODE{"cpp"}% #include
&lt;stdio.h&gt; #include <iostream></iostream>using namespace std; </strong> </p>
<p><strong> class Test { public: Test(int a=0,int b=0) { Test::a=a;
Test::b=b; } int a; int b; }; int main() { Test t(100,50);
printf("%???",t);//不明确的输出格式 scanf("%???",t);//不明确的输入格式
cout&lt;&lt;t&lt;&lt;endl;//同样不够明确 cin&gt;&gt;t;//同样不够明确
system("pause"); } %ENDCODE%
由于自定义类的特殊性，在上面的代码中，无论你使用c风格的输入输出，或者是c++的输入输出都不是不明确的一个表示，由于c语言没有运算符重载机
制，导致stdio库的不可扩充性，让我们无法让printf()和scanf()支持对自定义类对象的扩充识别，而c++是可以通过运算符重载机制扩充
iostream库的，使系统能能够识别自定义类型，从而让输入输出明确的知道他们该干什么，格式是什么。 </strong> </p>
<p><strong> 　　在上例中我们之所以用printf与cout进行对比目的是为了告诉大家，C与C++处理输入输出的根本不同，我们从c远的输
入输出可以很明显看出是函数调用方式，而c++的则是对象模式，cout和cin是ostream类和istream类的对象。 </strong> </p>
<h3><a name="2 fstream: ifstream 和 ofstream"></a>1
iostream: istream 和 ostream </h3>
<strong> 　　C++中的iostream库主要包含下图所示的几个头文件: </strong>
<table class="twikiTable" border="0" cellpadding="1" cellspacing="1">
    <tbody>
        <tr class="twikiTableEven">
            <th class="twikiFirstCol" colspan="2" maxcols="0" bgcolor="#dadada">IOSstream 库 </th>
        </tr>
        <tr class="twikiTableOdd">
            <td class="twikiFirstCol" bgcolor="#eaeaea"><strong> fstream </strong> </td>
            <td bgcolor="#eaeaea"><strong> iomainip </strong> </td>
        </tr>
        <tr class="twikiTableEven">
            <td class="twikiFirstCol" bgcolor="#ffffff"><strong> ios </strong> </td>
            <td bgcolor="#ffffff"><strong> iosfwd </strong> </td>
        </tr>
        <tr class="twikiTableOdd">
            <td class="twikiFirstCol" bgcolor="#eaeaea"><strong> iostream </strong> </td>
            <td bgcolor="#eaeaea"><strong> istream </strong> </td>
        </tr>
        <tr class="twikiTableEven">
            <td class="twikiFirstCol" bgcolor="#ffffff"><strong> ostream </strong> </td>
            <td bgcolor="#ffffff"><strong> sstream </strong> </td>
        </tr>
        <tr class="twikiTableOdd">
            <td class="twikiFirstCol twikilast" bgcolor="#eaeaea"><strong> streambuf </strong> </td>
            <td class="twikiLast" bgcolor="#eaeaea"><strong> strstream </strong> </td>
        </tr>
    </tbody>
</table>
<p><strong> 　　我们所熟悉的输入输出操作分别是由istream(输入流)和ostream(输出流)这两个类提供的，为了允许双向的输入／
输出，由istream和ostream派生出了iostream类。 </strong> </p>
<p><strong> 　　类的继承关系见下图：<br><img alt="" src="http://www.pconline.com.cn/pcedu/empolder/gj/c/0504/pic/05cppios02.gif">
</strong> </p>
<p><strong> iostream库定义了以下三个标准流对象： </strong> </p>
<ol>
    <li><strong> cin，表示标准输入(standard input)的istream类对象。cin使我们可以从设备读如数据。 </strong>
    </li>
    <li><strong> cout，表示标准输出(standard
    output)的ostream类对象。cout使我们可以向设备输出或者写数据。 </strong>
    </li>
    <li><strong> cerr，表示标准错误(standard
    error)的osttream类对象。cerr是导出程序错误消息的地方，它只能允许向屏幕设备写数据。 </strong> </li>
</ol>
<p><strong> 　　输出主要由重载的左移操作符（&lt;&lt;）来完成，输入主要由重载的右移操作符(&gt;&gt;)完成: </strong> </p>
<ol>
    <li><strong> &gt;&gt;a表示将数据放入a对象中。 </strong>
    </li>
    <li><strong> &lt;&lt;a表示将a对象中存储的数据拿出。 </strong> </li>
</ol>
<p><strong> 　　这些标准的流对象都有默认的所对应的设备，见下表：<br></strong>
<table class="twikiTable" border="0" cellpadding="1" cellspacing="1">
    <tbody>
        <tr class="twikiTableEven">
            <th class="twikiFirstCol" maxcols="0" bgcolor="#dadada"><a  href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?sortcol=0;table=2;up=0#sorted_table" title="Sort by
            this column" style="color: #000000;" rel="nofollow"><u>C++对象名</u></a> </th>
            <th maxcols="0" bgcolor="#dadada"><a title="Sort by this column" style="color: #000000;" href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?sortcol=1;table=2;up=0#sorted_table" rel="nofollow"><u>设备名称</u></a> </th>
            <th maxcols="0" bgcolor="#dadada"><a title="Sort by this column" style="color: #000000;" href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?sortcol=2;table=2;up=0#sorted_table" rel="nofollow"><u>C中标准设备名</u></a> </th>
            <th maxcols="0" bgcolor="#dadada"><a title="Sort by this column" style="color: #000000;" href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?sortcol=3;table=2;up=0#sorted_table" rel="nofollow"><u>默认含义</u></a> </th>
        </tr>
        <tr class="twikiTableOdd">
            <td class="twikiFirstCol" align="middle" bgcolor="#eaeaea"><strong> cin </strong> </td>
            <td align="middle" bgcolor="#eaeaea"><strong> 键盘 </strong> </td>
            <td align="middle" bgcolor="#eaeaea"><strong> stdin </strong> </td>
            <td align="middle" bgcolor="#eaeaea"><strong> 标准输入 </strong> </td>
        </tr>
        <tr class="twikiTableEven">
            <td class="twikiFirstCol" align="middle" bgcolor="#ffffff"><strong> cout </strong> </td>
            <td align="middle" bgcolor="#ffffff"><strong> 显示器屏幕 </strong> </td>
            <td align="middle" bgcolor="#ffffff"><strong> stdout </strong> </td>
            <td align="middle" bgcolor="#ffffff"><strong> 标准输出 </strong> </td>
        </tr>
        <tr class="twikiTableOdd">
            <td class="twikiFirstCol twikilast" align="middle" bgcolor="#eaeaea"><strong> cerr
            </strong> </td>
            <td class="twikiLast" align="middle" bgcolor="#eaeaea"><strong> 显示器屏幕 </strong> </td>
            <td class="twikiLast" align="middle" bgcolor="#eaeaea"><strong> stderr </strong> </td>
            <td class="twikiLast" align="middle" bgcolor="#eaeaea"><strong> 标准错误输出 </strong> </td>
        </tr>
    </tbody>
</table>
<strong> 　　上表中的意思表明cin对象的默认输入设备是键盘，cout对象的默认输出设备是显示器屏幕。 </strong> </p>
<p><strong> 　　那么原理上Ｃ++有是如何利用cin／cout对象与左移和右移运算符重载来实现输入输出的呢？ </strong> </p>
<p><strong> 　　下面我们以输出为例，说明其实现原理： </strong> </p>
<ol>
    <li><strong> cout是ostream类的对象，因为它所指向的是标准设备（显示器屏幕），所以它在iostream头文件中作为全
    局对象进行定义。 </strong>
    </li>
    <li><strong> ostream cout(stdout);//其默认指向的C中的标准设备名，作为其构造函数的参数使用。
    </strong>
    </li>
    <li><strong> 在iostream.h头文件中，ostream类对应每个基本数据类型都有其友元函数对左移操作符进行了友
    元函数的重载。 </strong>
    <ul>
        <li><strong> ostream&amp; operator&lt;&lt;(ostream &amp;temp,int
        source); </strong>
        </li>
        <li><strong> ostream&amp; operator&lt;&lt;(ostream
        &amp;temp,char *ps); </strong>
        </li>
        <li><strong> ... 等等 </strong> </li>
    </ul>
    </li>
</ol>
<p><strong> 　　一句输出语句：cout&lt;&lt;"www.cndev-lab.com"；，事实上调用的就是
ostream&amp; operator&lt;&lt;(ostream &amp;temp,char
*ps);这个运算符重载函数，由于返回的是流对象的引用，引用可以作为左值使用，所以当程序中有类似cout&lt;&lt;"www.cndev-
lab.com"&lt;&lt;"中国软件开发实验室";这样的语句出现的时候，就能够构成连续输出。 </strong> </p>
<p><strong> 　　由于iostream库不光支持对象的输入输出，同时也支持文件流的输入输出，所以在详细讲解左移与右移运算符重载只前，我们
有必要先对文件的输入输出以及输入输出的控制符有所了解。 </strong> </p>
<h3><a name="2 fstream: ifstream 和 ofstream"></a>2 fstream: ifstream 和
ofstream </h3>
<strong> 　　和文件有关系的输入输出类主要在fstream.h这个头文件中被定义，在这个头文件中主要被定义了三个类，由这三个类控制对文件的
各种输入输出操作，他们分别是ifstream、ofstream、fstream，其中fstream类是由iostream类派生而来，他们之间的继
承关系见下图所示。<br><img alt="" src="http://www.pconline.com.cn/pcedu/empolder/gj/c/0504/pic/05cppios04.gif">
</strong>
<p><strong> 由于文件设备并不像显示器屏幕与键盘那样是标准默认设备，所以它在fstream.h头文件中是没有像cout那样预先定义的全局
对象，所以我们必须自己定义一个该类的对象，我们要以文件作为设备向文件输出信息(也就是向文件写数据)，那么就应该使用ofstream类。 </strong> </p>
<p><strong> 　　ofstream类的默认构造函数原形为： %CODE{"cpp"}%
ofstream::ofstream(const char *filename,int mode = ios::out,int
openprot = filebuf::openprot); %ENDCODE% </strong> </p>
<ul>
    <li><strong> filename：　　要打开的文件名 </strong>
    </li>
    <li><strong> mode：　　　　要打开文件的方式 </strong>
    </li>
    <li><strong> prot：　　　　打开文件的属性 </strong> </li>
</ul>
<p><strong> 　　其中mode和openprot这两个参数的可选项表见下表： </strong>
<table class="twikiTable" border="0" cellpadding="1" cellspacing="1">
    <tbody>
        <tr class="twikiTableEven">
            <td class="twikiFirstCol" colspan="2" align="middle" bgcolor="#eaeaea"><strong> mode
            属性表 </strong> </td>
        </tr>
        <tr class="twikiTableOdd">
            <td class="twikiFirstCol" bgcolor="#ffffff"><strong> ios::app </strong> </td>
            <td bgcolor="#ffffff"><strong> 以追加的方式打开文件 </strong> </td>
        </tr>
        <tr class="twikiTableEven">
            <td class="twikiFirstCol" bgcolor="#eaeaea"><strong> ios::ate </strong> </td>
            <td bgcolor="#eaeaea"><strong> 文件打开后定位到文件尾，ios:app就包含有此属性 </strong> </td>
        </tr>
        <tr class="twikiTableOdd">
            <td class="twikiFirstCol" bgcolor="#ffffff"><strong> ios::binary </strong> </td>
            <td bgcolor="#ffffff"><strong> 以二进制方式打开文件，缺省的方式是文本方式。两种方式的区别见前文 </strong> </td>
        </tr>
        <tr class="twikiTableEven">
            <td class="twikiFirstCol" bgcolor="#eaeaea"><strong> ios::in </strong> </td>
            <td bgcolor="#eaeaea"><strong> 文件以输入方式打开 </strong> </td>
        </tr>
        <tr class="twikiTableOdd">
            <td class="twikiFirstCol" bgcolor="#ffffff"><strong> ios::out </strong> </td>
            <td bgcolor="#ffffff"><strong> 文件以输出方式打开 </strong> </td>
        </tr>
        <tr class="twikiTableEven">
            <td class="twikiFirstCol twikilast" bgcolor="#eaeaea"><strong> ios::trunc </strong> </td>
            <td class="twikiLast" bgcolor="#eaeaea"><strong> 如果文件存在，把文件长度设为0 </strong> </td>
        </tr>
    </tbody>
</table>
<strong> 　　可以用&#8220;或&#8221;把以上属性连接起来，如ios::out|ios::binary。 </strong>
<table class="twikiTable" border="0" cellpadding="1" cellspacing="1">
    <tbody>
        <tr class="twikiTableEven">
            <td class="twikiFirstCol" colspan="2" align="middle" bgcolor="#eaeaea"><strong> openprot
            属性表 </strong> </td>
        </tr>
        <tr class="twikiTableOdd">
            <td class="twikiFirstCol" bgcolor="#ffffff"><strong> 属性 </strong> </td>
            <td align="middle" bgcolor="#ffffff"><strong> 含义 </strong> </td>
        </tr>
        <tr class="twikiTableEven">
            <td class="twikiFirstCol" bgcolor="#eaeaea"><strong> 0 </strong> </td>
            <td bgcolor="#eaeaea"><strong> 普通文件，打开访问 </strong> </td>
        </tr>
        <tr class="twikiTableOdd">
            <td class="twikiFirstCol" bgcolor="#ffffff"><strong> 1 </strong> </td>
            <td bgcolor="#ffffff"><strong> 只读文件 </strong> </td>
        </tr>
        <tr class="twikiTableEven">
            <td class="twikiFirstCol" bgcolor="#eaeaea"><strong> 2 </strong> </td>
            <td bgcolor="#eaeaea"><strong> 隐含文件 </strong> </td>
        </tr>
        <tr class="twikiTableOdd">
            <td class="twikiFirstCol twikilast" bgcolor="#ffffff"><strong> 4 </strong> </td>
            <td class="twikiLast" bgcolor="#ffffff"><strong> 系统文件 </strong> </td>
        </tr>
    </tbody>
</table>
<strong> 　　可以用&#8220;或&#8221;或者&#8220;+&#8221;把以上属性连接起来 ，如3或1|2就是以只读和隐含属性打开文件。 </strong> </p>
<p><strong> 实例代码如下： %CODE{"cpp"}% #include <fstream></fstream>using
namespace std; </strong> </p>
<p><strong> int main() { ofstream
myfile("c:\\1.txt",ios::out|ios::trunc,0);
myfile&lt;&lt;"中国软件开发实验室"&lt;&lt;endl&lt;&lt;"网址："&lt;&lt;"www.cndev-
lab.com"; myfile.close() system("pause"); } %ENDCODE%
文件使用完后可以使用close成员函数关闭文件。 </strong> </p>
<p><strong> 　　ios::app为追加模式，在使用追加模式的时候同时进行文件状态的判断是一个比较好的习惯。 </strong> </p>
<p><strong> 　　示例如下： </strong> </p>
<p><strong> %CODE{"cpp"}% #include <iostream></iostream>#include <fstream></fstream>using
namespace std; int main() { ofstream myfile("c:\\1.txt",ios::app,0);
if(myfile)//或者写成myfile.fail() { cout&lt;&lt;"文件打开失败，目标文件状态可能为只读！";
system("pause"); exit(1); }
myfile&lt;&lt;"中国软件开发实验室"&lt;&lt;endl&lt;&lt;"网址："&lt;&lt;"www.cndev-
lab.com"&lt;&lt;endl; myfile.close(); } %ENDCODE% </strong> </p>
<p><strong> 　　在定义ifstream和ofstream类对象的时候，我们也可以不指定文件。以后可以通过成员函数open()显式的把一
个文件连接到一个类对象上。 </strong> </p>
<p><strong> 　　例如： </strong> </p>
<p><strong> %CODE{"cpp"}% #include <iostream></iostream>#include <fstream></fstream>using
namespace std; int main() { ofstream myfile;
myfile.open("c:\\1.txt",ios::out|ios::app,0);
if(myfile)//或者写成myfile.fail() { cout&lt;&lt;"文件创建失败,磁盘不可写或者文件为只读!";
system("pause"); exit(1); }
myfile&lt;&lt;"中国软件开发实验室"&lt;&lt;endl&lt;&lt;"网址："&lt;&lt;"www.cndev-
lab.com"&lt;&lt;endl; myfile.close(); } %ENDCODE%
下面我们来看一下是如何利用ifstream类对象，将文件中的数据读取出来，然后再输出到标准设备中的例子。 </strong> </p>
<p><strong> 　　代码如下： %CODE{"cpp"}% #include <iostream></iostream>#include <fstream></fstream>#include
<string></string>using namespace std; int main() { ifstream myfile;
myfile.open("c:\\1.txt",ios::in,0); if(myfile) { cout&lt;&lt;"文件读错误";
system("pause"); exit(1); } char ch; string content;
while(myfile.get(ch)) { content+=ch;
cout.put(ch);//cout&lt;&lt;ch;这么写也是可以的 } myfile.close();
cout&lt;&lt;content; system("pause"); } %ENDCODE%
上例中，我们利用成员函数get()，逐一的读取文件中的有效字符，再利用put()成员函数，将文件中的数据通过循环逐一输出到标准设备(屏幕)
上，
get()成员函数会在文件读到默尾的时候返回假值，所以我们可以利用它的这个特性作为while循环的终止条件，我们同时也在上例中引入了C++风格的
字符串类型string，在循环读取的时候逐一保存到content中，要使用string类型，必须包含string.h的头文件。 </strong> </p>
<p><strong> &nbsp;</strong> </p>
<p><strong> 我们在简单介绍过ofstream类和ifstream类后，我们再来看一下fstream类，fstream类是由
iostream派生而来，fstream类对象可以同对文件进行读写操作。 </strong> </p>
<p><strong> 　　示例代码如下： %CODE{"cpp"}% #include <iostream></iostream>#include
<fstream></fstream>using namespace std; int main() { fstream myfile;
myfile.open("c:\\1.txt",ios::out|ios::app,0); if(myfile) {
cout&lt;&lt;"文件写错误,文件属性可能为只读!"&lt;&lt;endl; system("pause"); exit(1); }
myfile&lt;&lt;"中国软件开发实验室"&lt;&lt;endl&lt;&lt;"网址："&lt;&lt;"www.cndev-
lab.com"&lt;&lt;endl; myfile.close(); </strong> </p>
<p><strong> myfile.open("c:\\1.txt",ios::in,0); if(myfile) {
cout&lt;&lt;"文件读错误,文件可能丢失!"&lt;&lt;endl; system("pause"); exit(1); }
char ch; while(myfile.get(ch)) { cout.put(ch); } myfile.close();
system("pause"); } %ENDCODE%
由于fstream类可以对文件同时进行读写操作，所以对它的对象进行初始话的时候一定要显式的指定mode和openprot参数。 </strong> </p>
<p><strong> 　　接下来我们来学习一下串流类的基础知识，什么叫串流类？ </strong> </p>
<h3><a name="3 strstream: ostrstream 和 istrs"></a>3 strstream:
ostrstream 和 istrstream </h3>
<strong> 　　简单的理解就是能够控制字符串类型对象进行输入输出的类，C++不光可以支持C++风格的字符串流控制，还可以支持C风格的字符串流
控制。 </strong>
<p><strong> 　　我们先看看看C++是如何对C风格的字符串流进行控制的，C中的字符串其实也就是字符数组，字符数组内的数据在内存中的位置的
排列是连续的，我们通常用 char str[size]或者char
*str的方式声明创建C风格字符数组，为了能让字符数组作为设备并提供输入输出操作，C++引入了ostrstream、istrstream、
strstream这三个类，要使用他们创建对象就必须包含strstream.h头文件。 </strong> </p>
<ul>
    <li><strong> istrstream类用于执行C风格的串流的输入操作，也就是以字符串数组作为输入设备。 </strong>
    </li>
    <li><strong> ostrstream类用于执行C风格的串流的输出操作，也就是一字符串数组作为输出设备。 </strong>
    </li>
    <li><strong> strstream类同时可以支持C风格的串流的输入输出操作。 </strong> </li>
</ul>
<p><strong> 　　istrstream类是从istream（输入流类）和strstreambase（字符串流基类）派生而
来，ostrstream是从
ostream（输出流类）和strstreambase（字符串流基类）派生而来，strstream则是从iostream(输入输出流类)和和
strstreambase（字符串流基类）派生而来。 </strong> </p>
<p><strong> 　　他们的继承关系如下图所示:<br><img alt="" src="http://www.pconline.com.cn/pcedu/empolder/gj/c/0504/pic/05cppios05.gif">
</strong> </p>
<p><strong> 　　串流同样不是标准设备，不会有预先定义好的全局对象，所以不能直接操作，需要通过构造函数创建对象。 </strong> </p>
<p><strong> 类istrstream的构造函数原形如下： %CODE{"cpp"}%
istrstream::istrstream(const char *str,int size); %ENDCODE%
参数1表示字符串数组,而参数2表示数组大小，当size为0时，表示istrstream类对象直接连接到由str所指向的内存空间并以\0结尾的
字符串。 </strong> </p>
<p><strong> 　　下面的示例代码就是利用istrstream类创建类对象，制定流输入设备为字符串数组，通过它向一个字符型对象输入数据。代
码如下： %CODE{"cpp"}% #include <iostream></iostream>#include <strstream></strstream>using
namespace std; int main() { char *name = "www.cndev-lab.com"; int
arraysize = strlen(name)+1; istrstream is(name,arraysize); char temp;
is&gt;&gt;temp; cout&lt;&lt;temp; system("pause"); } %ENDCODE%
类ostrstream用于执行串流的输出，它的构造函数如下所示： %CODE{"cpp"}%
ostrstream::ostrstream(char *_Ptr,int streamsize,int Mode = ios::out);
%ENDCODE% 　　第一个参数是字符数组，第二个是说明数组的大小，第三个参数是指打开方式。 </strong> </p>
<p><strong> 　　我们来一个示例代码： %CODE{"cpp"}% #include <iostream></iostream>#include
<strstream></strstream>using namespace std; int main() { int
arraysize=1; char *pbuffer=new char[arraysize]; ostrstream
ostr(pbuffer,arraysize,ios::out);
ostr&lt;&lt;arraysize&lt;&lt;ends;//使用ostrstream输出到流对象的时候,要用ends结束字符串
cout&lt;&lt;pbuffer; delete[] pbuffer; system("pause"); } %ENDCODE%
上面的代码中，我们创建一个c风格的串流输出对象ostr，我们将arraysize内的数据成功的以字符串的形式输出到了ostr对象所指向的
pbuffer指针的堆空间中，pbuffer也正是我们要输出的字符串数组，在结尾要使用ends结束字符串，如果不这么做就有溢出的危险。 </strong> </p>
<h3><a name="4 stringstream"></a>4 stringstream </h3>
<strong> 对于stringstream了来说，不用我多说，大家也已经知道它是用于C++风格的字符串的输入输出的。
stringstream的构造函数原形如下： %CODE{"cpp"}% 　　stringstream::stringstream(string
str); %ENDCODE% 　　示例代码如下: %CODE{"cpp"}% #include <iostream></iostream>#include
<sstream></sstream>#include <string></string>using namespace std; </strong>
<p><strong> int main() { stringstream ostr("ccc"); ostr.put('d');
ostr.put('e'); ostr&lt;&lt;"fg"; string gstr = ostr.str();
cout&lt;&lt;gstr&lt;&lt;endl; </strong> </p>
<p><strong> char a; ostr&gt;&gt;a; cout&lt;&lt;a </strong> </p>
<p><strong> system("pause"); } %ENDCODE%
除此而外，stringstream类的对象我们还常用它进行string与各种内置类型数据之间的转换。示例代码如下：
%CODE{"cpp"}% #include <iostream></iostream>#include <sstream></sstream>#include
<string></string>using namespace std; </strong> </p>
<p><strong> int main() { stringstream sstr;
//--------int转string----------- int a=100; string str; sstr&lt;&lt;a;
sstr&gt;&gt;str; cout&lt;&lt;str&lt;&lt;endl;
//--------string转char[]--------
sstr.clear();//如果你想通过使用同一stringstream对象实现多种类型的转换，请注意在每一次转换之后都必须调用clear()
成员函数。 string name = "colinguan"; char cname[200]; sstr&lt;&lt;name;
sstr&gt;&gt;cname; cout&lt;&lt;cname; system("pause"); } %ENDCODE%
接下来我们来学习一下输入/输出的状态标志的相关知识. </strong> </p>
<h3><a name="5 io_state 输入/输出的状态标志"></a>5 io_state 输入/输出的状态标志 </h3>
<strong> C++中负责的输入/输出的系统包括了关于每一个输入/输出操作的结果的记录信息。这些当前的状态信息被包含在io_state类型的对
象中。io_state是一个枚举类型（就像open_mode一样），以下便是它包含的值。 </strong>
<ul>
    <li><strong> goodbit 无错误 </strong>
    </li>
    <li><strong> Eofbit 已到达文件尾 </strong>
    </li>
    <li><strong> failbit 非致命的输入/输出错误，可挽回 </strong>
    </li>
    <li><strong> badbit　致命的输入/输出错误,无法挽回 </strong> </li>
</ul>
<p><strong> 有两种方法可以获得输入/输出的状态信息。一种方法是通过调用rdstate()函数，它将返回当前状态的错误标记。例如，假如没
有任何错误，则rdstate()会返回goodbit.下例示例，表示出了rdstate()的用法： %CODE{"cpp"}% #include
<iostream></iostream>using namespace std; </strong> </p>
<p><strong> int main() { int a; cin&gt;&gt;a;
cout&lt;&lt;cin.rdstate()&lt;&lt;endl; if(cin.rdstate() == ios::goodbit)
{ cout&lt;&lt;"输入数据的类型正确，无错误！"&lt;&lt;endl; } if(cin.rdstate() ==
ios_base::failbit) {
cout&lt;&lt;"输入数据类型错误，非致命错误，可清除输入缓冲区挽回！"&lt;&lt;endl; } system("pause");
} %ENDCODE% 　　另一种方法则是使用下面任何一个函数来检测相应的输入/输出状态： %CODE{"cpp"}% bool bad();
bool eof(); bool fail(); bool good(); %ENDCODE% </strong> </p>
<p><strong> 　　下例示例，表示出了上面各成员函数的用法： %CODE{"cpp"}% #include <iostream></iostream>using
namespace std; </strong> </p>
<p><strong> int main() { int a; cin&gt;&gt;a;
cout&lt;&lt;cin.rdstate()&lt;&lt;endl; if(cin.good()) {
cout&lt;&lt;"输入数据的类型正确，无错误！"&lt;&lt;endl; } if(cin.fail()) {
cout&lt;&lt;"输入数据类型错误，非致命错误，可清除输入缓冲区挽回！"&lt;&lt;endl; } system("pause");
} %ENDCODE%
如果错误发生，那么流状态既被标记为错误，你必须清除这些错误状态，以使你的程序能正确适当地继续运行。要清除错误状态，需使用clear()函数。
此函数带一个参数，它是你将要设为当前状态的标志值。，只要将ios::goodbit作为实参。 </strong> </p>
<p><strong> 　　示例代码如下： %CODE{"cpp"}% #include <iostream></iostream>using
namespace std; </strong> </p>
<p><strong> int main() { int a; cin&gt;&gt;a;
cout&lt;&lt;cin.rdstate()&lt;&lt;endl; cin.clear(ios::goodbit);
cout&lt;&lt;cin.rdstate()&lt;&lt;endl; system("pause"); } %ENDCODE%
通常当我们发现输入有错又需要改正的时候，使用clear()更改标记为正确后，同时也需要使用get()成员函数清除输入缓冲区，以达到重复输入的目
的。 </strong> </p>
<p><strong> 　　示例代码如下： %CODE{"cpp"}% #include <iostream></iostream>using
namespace std; </strong> </p>
<p><strong> int main() { int a; while(1) { cin&gt;&gt;a;
if(cin)//条件可改写为cin.fail() { cout&lt;&lt;"输入有错!请重新输入"&lt;&lt;endl;
cin.clear(); cin.get(); } else { cout&lt;&lt;a; break; } }
system("pause"); } %ENDCODE% 　　最后再给出一个对文件流错误标记处理的例子，巩固学习，代码如下：
%CODE{"cpp"}% #include <iostream></iostream>#include <fstream></fstream>using
namespace std; </strong> </p>
<p><strong> int main() { ifstream myfile("c:\\1.txt",ios_base::in,0);
if(myfile.fail()) { cout&lt;&lt;"文件读取失败或指定文件不存在!"&lt;&lt;endl; } else {
char ch; while(myfile.get(ch)) { cout&lt;&lt;ch; } if(myfile.eof()) {
cout&lt;&lt;"文件内容已经全部读完"&lt;&lt;endl; } while(myfile.get(ch)) {
cout&lt;&lt;ch; } } system("pause"); } %ENDCODE%&nbsp;&nbsp;</strong> </p>
<br> <img src ="http://www.cppblog.com/yuqilin1228/aggbug/110620.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuqilin1228/" target="_blank">LynnRaymond</a> 2010-03-26 22:03 <a href="http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110620.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【C++常识】C++输入输出流</title><link>http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110619.html</link><dc:creator>LynnRaymond</dc:creator><author>LynnRaymond</author><pubDate>Fri, 26 Mar 2010 14:01:00 GMT</pubDate><guid>http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110619.html</guid><wfw:comment>http://www.cppblog.com/yuqilin1228/comments/110619.html</wfw:comment><comments>http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110619.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuqilin1228/comments/commentRss/110619.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuqilin1228/services/trackbacks/110619.html</trackback:ping><description><![CDATA[<p>C++ 通过以下几个类支持文件的输入输出：</p>
<ul>
    <li>ofstream: 写操作（输出）的文件类 (由ostream引申而来)
    </li>
    <li>ifstream: 读操作（输入）的文件类(由istream引申而来)
    </li>
    <li>fstream: 可同时读写操作的文件类 (由iostream引申而来) </li>
</ul>
<p>&nbsp;</p>
<h3>打开文件(Open a file)</h3>
<p>对这些类的一个对象所做的第一个操作通常就是将它和一个真正的文件联系起来，也就是说打开一个文件。被打开的文件在程序中由一个流对象
(stream object)来表示 (这些类的一个实例) ，而对这个流对象所做的任何输入输出操作实际就是对该文件所做的操作。</p>
<p>要通过一个流对象打开一个文件，我们使用它的成员函数open()：</p>
<p><code><font face="NSimsun">void open (const char * filename, openmode
mode);</font></code>这里filename 是一个字符串，代表要打开的文件名，mode 是以下标志符的一个组合：</p>
<table border="1" cellpadding="3" cellspacing="0">
    <tbody>
        <tr>
            <td>ios::in</td>
            <td>为输入(读)而打开
            文件</td>
        </tr>
        <tr>
            <td>ios::out</td>
            <td>为输出(写)而打开
            文件</td>
        </tr>
        <tr>
            <td>ios::ate</td>
            <td>初始位置：文件尾</td>
        </tr>
        <tr>
            <td>ios::app</td>
            <td>所有输出附加在文件
            末尾</td>
        </tr>
        <tr>
            <td>ios::trunc</td>
            <td>如果文件已存在则先
            删除该文件</td>
        </tr>
        <tr>
            <td>ios::binary</td>
            <td>二进制方式</td>
        </tr>
    </tbody>
</table>
<p>这些标识符可以被组合使用，中间以&#8221;或&#8221;操作符(|)间隔。例如，如果我们想要以二进制方式打开文件&#8221;example.bin&#8221;
来写入一些数据，我们可以通过以下方式调用成员函数open（）来实现：</p>
<p><code><font face="NSimsun">ofstream file;<br>file.open
("example.bin", ios::out | ios::app | ios::binary); </font></code>ofstream,
ifstream 和 fstream所有这些类的成员函数open 都包含了一个默认打开文件的方式，这三个类的默认方式各不相同：</p>
<table border="1" cellpadding="3" cellspacing="0">
    <tbody>
        <tr>
            <th>类</th>
            <th>参数的默认方式</th>
        </tr>
        <tr>
            <td>ofstream</td>
            <td>ios::out |
            ios::trunc</td>
        </tr>
        <tr>
            <td>ifstream</td>
            <td>ios::in</td>
        </tr>
        <tr>
            <td>fstream</td>
            <td>ios::in |
            ios::out</td>
        </tr>
    </tbody>
</table>
<p>只有当函数被调用时没有声明方式参数的情况下，默认值才会被采用。如果函数被调用时声明了任何参数，默认值将被完全改写，而不会与调用参数组合。</p>
<p>由于对类ofstream, ifstream 和 fstream
的对象所进行的第一个操作通常都是打开文件，这些类都有一个构造函数可以直接调用open
函数，并拥有同样的参数。这样，我们就可以通过以下方式进行与上面同样的定义对象和打开文件的操作：</p>
<p><code><font face="NSimsun">ofstream file ("example.bin", ios::out |
ios::app | ios::binary);</font></code>两种打开文件的方式都是正确的。</p>
<p>你可以通过调用成员函数is_open()来检查一个文件是否已经被顺利的打开了：</p>
<p><code><font face="NSimsun">bool is_open();</font></code>它返回一个布尔(bool)
值，为真（true）代表文件已经被顺利打开，假( false )则相反。</p>
<h3>关闭文件(Closing a file)</h3>
<p>当文件读写操作完成之后，我们必须将文件关闭以使文件重新变为可访问的。关闭文件需要调用成员函数close()，它负责将缓存中的数据排放出来并
关闭文件。它的格式很简单：</p>
<p><code><font face="NSimsun">void close ();</font></code>这个函数一旦被调用，原先的流
对象(stream object)就可以被用来打开其它的文件了，这个文件也就可以重新被其它的进程(process)所有访问了。</p>
<p>为防止流对象被销毁时还联系着打开的文件，析构函数(destructor)将会自动调用关闭函数close。</p>
<h3>文本文件(Text mode files)</h3>
<p>类ofstream, ifstream 和fstream 是分别从ostream, istream 和iostream
中引申而来的。这就是为什么 fstream 的对象可以使用其父类的成员来访问数据。</p>
<p>一般来说，我们将使用这些类与同控制台(console)交互同样的成员函数(cin 和
cout)来进行输入输出。如下面的例题所示，我们使用重载的插入操作符&lt;&lt;：</p>
<table border="2" cellpadding="1" cellspacing="1" width="400">
    <tbody>
        <tr>
            <td><font color="#008000">// writing on a text file</font><br>#include
            &lt;fiostream.h&gt;int main () {<br>ofstream examplefile
            (&#8221;example.txt&#8221;);<br>if (examplefile.is_open()) {<br>examplefile &lt;&lt;
            &#8220;This is a line.\n&#8221;;<br>examplefile &lt;&lt; &#8220;This is another line.\n&#8221;;<br>examplefile.close();<br>}<br>return
            0;<br>}</td>
            <td><font color="#008000">file example.txt<br></font>This is a line.<br>This is
            another line.</td>
        </tr>
    </tbody>
</table>
<p>从文件中读入数据也可以用与 cin的使用同样的方法：</p>
<table border="2" cellpadding="1" cellspacing="1" width="400">
    <tbody>
        <tr>
            <td><font color="#008000">// reading a text file</font><br>#include
            &lt;iostream.h&gt;<br>#include &lt;fstream.h&gt;<br>#include
            &lt;stdlib.h&gt;int main () {<br>char buffer[256];<br>ifstream
            examplefile (&#8221;example.txt&#8221;);<br>if (! examplefile.is_open())<br>{ cout
            &lt;&lt; &#8220;Error opening file&#8221;; exit (1); }<br>while (! examplefile.eof()
            ) {<br>examplefile.getline (buffer,100);<br>cout &lt;&lt; buffer
            &lt;&lt; endl;<br>}<br>return 0;<br>}</td>
            <td>This is a
            line.<br>This is another line.</td>
        </tr>
    </tbody>
</table>
<p>上面的例子读入一个文本文件的内容，然后将它打印到屏幕上。注意我们使用了一个新的成员函数叫做eof ，它是ifstream 从类 ios
中继承过来的，当到达文件末尾时返回true 。</p>
<h3>状态标志符的验证(Verification of state flags)</h3>
<p>除了eof()以外，还有一些验证流的状态的成员函数（所有都返回bool型返回值）：</p>
<ul>
    <li><strong> bad()</strong> 如果在读写过程中出错，返回 true
    。例如：当我们要对一个不是打开为写状态的文件进行写入时，或者我们要写入的设备没有剩余空间的时候。
    </li>
    <li><strong> fail()</strong> 除了与bad() 同样的情况下会返回 true
    以外，加上格式错误时也返回true ，例如当想要读入一个整数，而获得了一个字母的时候。
    </li>
    <li><strong> eof()</strong> 如果读文件到达文件末尾，返回true。
    </li>
    <li><strong> good()</strong> 这是最通用的：如果调用以上任何一个函数返回true 的话，此函数返回
    false 。 </li>
</ul>
<p>要想重置以上成员函数所检查的状态标志，你可以使用成员函数clear()，没有参数。</p>
<h3>获得和设置流指针(get and put stream pointers)</h3>
<p>所有输入/输出流对象(i/o streams objects)都有至少一个流指针：</p>
<ul>
    <li>ifstream， 类似istream, 有一个被称为get pointer的指针，指向下一个将被读取的元素。
    </li>
    <li>ofstream, 类似 ostream, 有一个指针 put pointer ，指向写入下一个元素的位置。
    </li>
    <li>fstream, 类似 iostream, 同时继承了get 和 put </li>
</ul>
<p>我们可以通过使用以下成员函数来读出或配置这些指向流中读写位置的流指针：</p>
<ul>
    <li><strong> tellg() 和 tellp()</strong> 这两个成员函数不用传入参数，返回pos_type
    类型的值(根据ANSI-C++ 标准) ，就是一个整数，代表当前get 流指针的位置 (用tellg) 或 put
    流指针的位置(用tellp).
    </li>
    <li><strong> seekg() 和seekp()</strong> 这对函数分别用来改变流指针get
    和put的位置。两个函数都被重载为两种不同的原型：
    <p>seekg ( pos_type position );<br>seekp ( pos_type
    position );</p>
    <p>使用这个原型，流指针被改变为指向从文件开始计算的一个绝对位置。要求传入的参数类型与函数 tellg 和tellp
    的返回值类型相同。</p>
    <p>seekg ( off_type offset, seekdir direction );<br>seekp
    ( off_type offset, seekdir direction );</p>
    <p>使用这个原型可以指定由参数direction决定的一个具体的指针开始计算的一个位移(offset)。它可以是：</p>
    <table border="1" cellpadding="3" cellspacing="0">
        <tbody>
            <tr>
                <td>ios::beg</td>
                <td>从流开始位
                置计算的位移</td>
            </tr>
            <tr>
                <td>ios::cur</td>
                <td>从流指针当
                前位置开始计算的位移</td>
            </tr>
            <tr>
                <td>ios::end</td>
                <td>从流末尾处
                开始计算的位移</td>
            </tr>
        </tbody>
    </table>
    </li>
</ul>
<p>流 指针 get 和 put 的值对文本文件(text file)和二进制文件(binary
file)的计算方法都是不同的，因为文本模式的文件中某些特殊字符可能被修改。由于这个原因，建议对以文本文件模式打开的文件总是使用seekg 和
seekp的第一种原型，而且不要对tellg 或 tellp
的返回值进行修改。对二进制文件，你可以任意使用这些函数，应该不会有任何意外的行为产生。</p>
<p>以下例子使用这些函数来获得一个二进制文件的大小：</p>
<table border="2" cellpadding="1" cellspacing="1" width="400">
    <tbody>
        <tr>
            <td><font color="#008000">// obtaining file size<br><font color="#000000">#include
            &lt;iostream.h&gt;<br>#include &lt;fstream.h&gt;</font></font><font color="#008000"><font color="#000000">const char * filename =
            &#8220;example.txt&#8221;;</font></font><font color="#008000"><font color="#000000">int
            main () {<br>long l,m;<br>ifstream file (filename,
            ios::in|ios::binary);<br>l = file.tellg();<br>file.seekg (0, ios::end);<br>m
            = file.tellg();<br>file.close();<br>cout &lt;&lt; &#8220;size of &#8221; &lt;&lt;
            filename;<br>cout &lt;&lt; &#8221; is &#8221; &lt;&lt; (m-l) &lt;&lt; &#8221; bytes.\n&#8221;;<br>return
            0;<br>}</font></font></td>
            <td>size of
            example.txt<br>is 40 bytes.</td>
        </tr>
    </tbody>
</table>
<h3>二进制文件(Binary files)</h3>
<p>在二进制文件中，使用&lt;&lt;
和&gt;&gt;，以及函数（如getline）来操作符输入和输出数据，没有什么实际意义，虽然它们是符合语法的。</p>
<p>文 件流包括两个为顺序读写数据特殊设计的成员函数：write 和 read。第一个函数 (write) 是ostream
的一个成员函数，都是被ofstream所继承。而read 是istream 的一个成员函数，被ifstream 所继承。类 fstream
的对象同时拥有这两个函数。它们的原型是：</p>
<p>write ( char * buffer, streamsize size );<br>read (
char * buffer, streamsize size );</p>
<p>这里 buffer 是一块内存的地址，用来存储或读出数据。参数size 是一个整数值，表示要从缓存（buffer）中读出或写入的字符数。</p>
<table border="2" cellpadding="1" cellspacing="1" width="400">
    <tbody>
        <tr>
            <td><font color="#008000"><font color="#008000">// reading binary file</font><br><font color="#000000">#include &lt;iostream&gt;<br>#include &lt;fstream.h&gt;</font></font><font color="#008000"><font color="#000000">const char * filename =
            &#8220;example.txt&#8221;;</font></font><font color="#008000"><font color="#000000">int
            main () {<br>char * buffer;<br>long size;<br>ifstream file (filename,
            ios::in|ios::binary|ios::ate);<br>size = file.tellg();<br>file.seekg (0,
            ios::beg);<br>buffer = new char [size];<br>file.read (buffer, size);<br>file.close();</font></font><font color="#008000"><font color="#000000">cout &lt;&lt; &#8220;the complete file
            is in a buffer&#8221;;</font></font><font color="#008000"><font color="#000000">delete[] buffer;<br>return 0;<br>}</font></font></td>
            <td>The
            complete file<br>is in a buffer</td>
        </tr>
    </tbody>
</table>
<h3>缓存和同步(Buffers and Synchronization)</h3>
<p>当 我们对文件流进行操作的时候，它们与一个streambuf
类型的缓存(buffer)联系在一起。这个缓存（buffer）实际是一块内存空间，作为流(stream)和物理文件的媒介。例如，对于一个输出流，
每次成员函数put
(写一个单个字符)被调用，这个字符不是直接被写入该输出流所对应的物理文件中的，而是首先被插入到该流的缓存（buffer）中。</p>
<p>当缓存被排放出来(flush)时，它里面的所有数据或者被写入物理媒质中（如果是一个输出流的话），或者简单的被抹掉(如果是一个输入流的话)。
这个过程称为同步(synchronization)，它会在以下任一情况下发生：</p>
<ul>
    <li><strong> 当文件被关闭时:</strong>  在文件被关闭之前，所有还没有被完全写出或读取的缓存都将被同步。
    </li>
    <li><strong> 当缓存buffer 满时:</strong> 缓存Buffers
    有一定的空间限制。当缓存满时，它会被自动同步。
    </li>
    <li><strong> 控制符明确指明:</strong> 当遇到流中某些特定的控制符时，同步会发生。这些控制符包括：flush
    和endl。
    </li>
    <li><strong> 明确调用函数sync():</strong>  调用成员函数sync()
    (无参数)可以引发立即同步。这个函数返回一个int 值，等于-1 表示流没有联系的缓存或操作失败。 </li>
</ul>
<br><img src ="http://www.cppblog.com/yuqilin1228/aggbug/110619.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuqilin1228/" target="_blank">LynnRaymond</a> 2010-03-26 22:01 <a href="http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110619.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【C++常识】C/C++头文件一览</title><link>http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110613.html</link><dc:creator>LynnRaymond</dc:creator><author>LynnRaymond</author><pubDate>Fri, 26 Mar 2010 13:39:00 GMT</pubDate><guid>http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110613.html</guid><wfw:comment>http://www.cppblog.com/yuqilin1228/comments/110613.html</wfw:comment><comments>http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110613.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yuqilin1228/comments/commentRss/110613.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yuqilin1228/services/trackbacks/110613.html</trackback:ping><description><![CDATA[<strong> <font size="2">C、传统 C++<br><br>#include
&lt;assert.h&gt;　　　　//设定插入点<br>#include &lt;ctype.h&gt;　　　　 //字符处理<br>#include
&lt;errno.h&gt;　　　　 //定义错误码<br>#include &lt;float.h&gt;　　　　 //浮点数处理<br>#include
&lt;fstream.h&gt;　　　 //文件输入／输出<br>#include &lt;iomanip.h&gt;
//参数化输入／输出<br>#include &lt;iostream.h&gt;　　　//数据流输入／输出<br>#include
&lt;limits.h&gt;　　　　//定义各种数据类型最值常量<br>#include
&lt;locale.h&gt;　　　　//定义本地化函数<br>#include &lt;math.h&gt;　　　　　//定义数学函数<br>#include
&lt;stdio.h&gt;　　　　 //定义输入／输出函数<br>#include
&lt;stdlib.h&gt;　　　　//定义杂项函数及内存分配函数<br>#include
&lt;string.h&gt;　　　　//字符串处理<br>#include
&lt;strstrea.h&gt;　　　//基于数组的输入／输出<br>#include
&lt;time.h&gt;　　　　　//定义关于时间的函数<br>#include &lt;wchar.h&gt;
//宽字符处理及输入／输出<br>#include &lt;wctype.h&gt;　　　　//宽字符分类<br><br>//////////////////////////////////////////////////////////////////////////<br><br>标
准 C++　（同上的不再注释）<br><br>#include &lt;algorithm&gt;　　　 //STL 通用算法<br>#include
&lt;bitset&gt;　　　　　//STL 位集容器<br>#include &lt;cctype&gt;<br>#include
&lt;cerrno&gt;<br>#include &lt;clocale&gt;<br>#include &lt;cmath&gt;<br>#include
&lt;complex&gt;　　　　 //复数类<br>#include &lt;cstdio&gt;<br>#include
&lt;cstdlib&gt;<br>#include &lt;cstring&gt;<br>#include &lt;ctime&gt;<br>#include
&lt;deque&gt;　　　　　 //STL 双端队列容器<br>#include &lt;exception&gt;
//异常处理类<br>#include &lt;fstream&gt;<br>#include
&lt;functional&gt;　　　//STL 定义运算函数（代替运算符）<br>#include &lt;limits&gt;<br>#include
&lt;list&gt;　　　　　　//STL 线性列表容器<br>#include &lt;map&gt;　　　　　　 //STL 映射容器<br>#include
&lt;iomanip&gt;<br>#include &lt;ios&gt;　　　　　　 //基本输入／输出支持<br>#include
&lt;iosfwd&gt;　　　　　//输入／输出系统使用的前置声明<br>#include &lt;iostream&gt;<br>#include
&lt;istream&gt;　　　　 //基本输入流<br>#include &lt;ostream&gt;　　　　 //基本输出流<br>#include
&lt;queue&gt;　　　　　 //STL 队列容器<br>#include &lt;set&gt;　　　　　　 //STL 集合容器<br>#include
&lt;sstream&gt;　　　　 //基于字符串的流<br>#include &lt;stack&gt;　　　　　 //STL
堆栈容器　　　　<br>#include &lt;stdexcept&gt;　　　 //标准异常类<br>#include
&lt;streambuf&gt;　　　 //底层输入／输出支持<br>#include &lt;string&gt;　　　　　//字符串类<br>#include
&lt;utility&gt;　　　　 //STL 通用模板类<br>#include &lt;vector&gt;　　　　　//STL
动态数组容器<br>#include &lt;cwchar&gt;<br>#include &lt;cwctype&gt;<br><br>using
namespace std;<br><br>//////////////////////////////////////////////////////////////////////////<br><br>C99
增加<br><br>#include &lt;complex.h&gt;　　 //复数处理<br>#include
&lt;fenv.h&gt;　　　　//浮点环境<br>#include &lt;inttypes.h&gt;　　//整数格式转换<br>#include
&lt;stdbool.h&gt;　　 //布尔环境<br>#include &lt;stdint.h&gt;　　　//整型环境<br>#include
&lt;tgmath.h&gt;　　　//通用类型数学宏</font>C/C++
头文件一览<br><!----><br></strong> <font size="2"><strong> C、传统 C++<br><br>#include &lt;assert.h&gt;　　　　//设定插入点<br>#include
&lt;ctype.h&gt;　　　　 //字符处理<br>#include &lt;errno.h&gt;　　　　 //定义错误码<br>#include
&lt;float.h&gt;　　　　 //浮点数处理<br>#include &lt;fstream.h&gt;　　　 //文件输入／输出<br>#include
&lt;iomanip.h&gt;　　　 //参数化输入／输出<br>#include
&lt;iostream.h&gt;　　　//数据流输入／输出<br>#include
&lt;limits.h&gt;　　　　//定义各种数据类型最值常量<br>#include
&lt;locale.h&gt;　　　　//定义本地化函数<br>#include &lt;math.h&gt;　　　　　//定义数学函数<br>#include
&lt;stdio.h&gt;　　　　 //定义输入／输出函数<br>#include
&lt;stdlib.h&gt;　　　　//定义杂项函数及内存分配函数<br>#include
&lt;string.h&gt;　　　　//字符串处理<br>#include
&lt;strstrea.h&gt;　　　//基于数组的输入／输出<br>#include
&lt;time.h&gt;　　　　　//定义关于时间的函数<br>#include &lt;wchar.h&gt;
//宽字符处理及输入／输出<br>#include &lt;wctype.h&gt;　　　　//宽字符分类<br><br>//////////////////////////////////////////////////////////////////////////<br><br>标
准 C++　（同上的不再注释）<br><br>#include &lt;algorithm&gt;　　　 //STL 通用算法<br>#include
&lt;bitset&gt;　　　　　//STL 位集容器<br>#include &lt;cctype&gt;<br>#include
&lt;cerrno&gt;<br>#include &lt;clocale&gt;<br>#include &lt;cmath&gt;<br>#include
&lt;complex&gt;　　　　 //复数类<br>#include &lt;cstdio&gt;<br>#include
&lt;cstdlib&gt;<br>#include &lt;cstring&gt;<br>#include &lt;ctime&gt;<br>#include
&lt;deque&gt;　　　　　 //STL 双端队列容器<br>#include &lt;exception&gt;
//异常处理类<br>#include &lt;fstream&gt;<br>#include
&lt;functional&gt;　　　//STL 定义运算函数（代替运算符）<br>#include &lt;limits&gt;<br>#include
&lt;list&gt;　　　　　　//STL 线性列表容器<br>#include &lt;map&gt;　　　　　　 //STL 映射容器<br>#include
&lt;iomanip&gt;<br>#include &lt;ios&gt;　　　　　　 //基本输入／输出支持<br>#include
&lt;iosfwd&gt;　　　　　//输入／输出系统使用的前置声明<br>#include &lt;iostream&gt;<br>#include
&lt;istream&gt;　　　　 //基本输入流<br>#include &lt;ostream&gt;　　　　 //基本输出流<br>#include
&lt;queue&gt;　　　　　 //STL 队列容器<br>#include &lt;set&gt;　　　　　　 //STL 集合容器<br>#include
&lt;sstream&gt;　　　　 //基于字符串的流<br>#include &lt;stack&gt;　　　　　 //STL
堆栈容器　　　　<br>#include &lt;stdexcept&gt;　　　 //标准异常类<br>#include
&lt;streambuf&gt;　　　 //底层输入／输出支持<br>#include &lt;string&gt;　　　　　//字符串类<br>#include
&lt;utility&gt;　　　　 //STL 通用模板类<br>#include &lt;vector&gt;　　　　　//STL
动态数组容器<br>#include &lt;cwchar&gt;<br>#include &lt;cwctype&gt;<br><br>using
namespace std;<br><br>//////////////////////////////////////////////////////////////////////////<br><br>C99
增加<br><br>#include &lt;complex.h&gt;　　 //复数处理<br>#include
&lt;fenv.h&gt;　　　　//浮点环境<br>#include &lt;inttypes.h&gt;　　//整数格式转换<br>#include
&lt;stdbool.h&gt;　　 //布尔环境<br>#include &lt;stdint.h&gt;　　　//整型环境<br>#include
&lt;tgmath.h&gt;　　　//通用类型数学宏</strong> </font><br>
<br>  <img src ="http://www.cppblog.com/yuqilin1228/aggbug/110613.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yuqilin1228/" target="_blank">LynnRaymond</a> 2010-03-26 21:39 <a href="http://www.cppblog.com/yuqilin1228/archive/2010/03/26/110613.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>