﻿<?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++博客-snowell-文章分类-技术类</title><link>http://www.cppblog.com/snowell/category/5017.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 22 May 2008 04:25:04 GMT</lastBuildDate><pubDate>Thu, 22 May 2008 04:25:04 GMT</pubDate><ttl>60</ttl><item><title> C++的IO标准库介绍(转) </title><link>http://www.cppblog.com/snowell/articles/30996.html</link><dc:creator>snowell</dc:creator><author>snowell</author><pubDate>Tue, 28 Aug 2007 02:46:00 GMT</pubDate><guid>http://www.cppblog.com/snowell/articles/30996.html</guid><wfw:comment>http://www.cppblog.com/snowell/comments/30996.html</wfw:comment><comments>http://www.cppblog.com/snowell/articles/30996.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/snowell/comments/commentRss/30996.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/snowell/services/trackbacks/30996.html</trackback:ping><description><![CDATA[<p><a href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro"><font color=#000080>http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro</font></a><br>作者：管宁 </p>
<div class=twikiToc>C++的iostream标准库介绍 <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 、为什么需要iostream <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1、 iostream: istream 和 ostream <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2 、fstream: ifstream 和 ofstream <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3、 strstream: ostrstream 和 istrstream <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4、 stringstream <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5、 io_state 输入/输出的状态标志 </div>
<h3><a name=0_iostream></a>0 为什么需要iostream </h3>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 我们从一开始就一直在利用C++的输入输出在做着各种练习，输入输出是由iostream库提供的，所以讨论此标准库是有必要的，它与C语言的 stdio库不同，它从一开始就是用多重继承与虚拟继承实现的面向对象的层次结构，作为一个c++的标准库组件提供给程序员使用。 </p>
<p>　　iostream为内置类型类型对象提供了输入输出支持，同时也支持文件的输入输出，类的设计者可以通过对iostream库的扩展，来支持自定义类型的输入输出操作。 </p>
<p>　　为什么说要扩展才能提供支持呢？我们来一个示例。 </p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;stdio.h&gt;</pre>
<pre><font color=#000080>#include</font> &lt;iostream&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;     </pre>
<pre><font color=#a52a2a>class</font> Test {    </pre>
<pre>     <font color=#a52a2a>public</font>:     </pre>
<pre>          Test(<font color=#a52a2a>int</font> a=0,<font color=#a52a2a>int</font> b=0)     </pre>
<pre>             { Test::a=a; </pre>
<pre>               Test::b=b;       </pre>
<pre>             }         </pre>
<pre>          <font color=#a52a2a>int</font> a;      </pre>
<pre>          <font color=#a52a2a>int</font> b; }; </pre>
<pre><font color=#a52a2a>int</font> main() { </pre>
<pre>        Test t(100,50); </pre>
<pre>        printf("<font color=#0000ff>%???</font>",t);<font color=#008000>//不明确的输出格式 <br></font>         scanf("<font color=#0000ff>%???</font>",t);<font color=#008000>//不明确的输入格式<br> </font>        cout&lt;&lt;t&lt;&lt;endl;<font color=#008000>//同样不够明确<br> </font>        cin&gt;&gt;t;<font color=#008000>//同样不够明确<br> </font>        system("<font color=#0000ff>pause</font>"); }</pre>
<pre>&nbsp;</pre>
</div>
<p>　　由于自定义类的特殊性，在上面的代码中，无论你使用c风格的输入输出，或者是c++的输入输出都不是不明确的一个表示，由于c语言没有运算符重载机制，导致stdio库的不可扩充性，让我们无法让printf()和scanf()支持对自定义类对象的扩充识别，而c++是可以通过运算符重载机制扩充 iostream库的，使系统能能够识别自定义类型，从而让输入输出明确的知道他们该干什么，格式是什么。 </p>
<p>　　在上例中我们之所以用printf与cout进行对比目的是为了告诉大家，C与C++处理输入输出的根本不同，我们从c远的输入输出可以很明显看出是函数调用方式，而c++的则是对象模式，cout和cin是ostream类和istream类的对象。 </p>
<h3><a name=1_iostream_istream_ostream></a>1 iostream: istream 和 ostream </h3>
<p>　　C++中的iostream库主要包含下图所示的几个头文件:
<table cellSpacing=1 cellPadding=0 border=1>
    <tbody>
        <tr>
            <th class=twikiFirstCol bgColor=#99cccc colSpan=2><strong>IOSstream 库</strong> </th>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffff>fstream </td>
            <td bgColor=#ffffff>iomainip </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffcc>ios </td>
            <td bgColor=#ffffcc>iosfwd </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffff>iostream </td>
            <td bgColor=#ffffff>istream </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffcc>ostream </td>
            <td bgColor=#ffffcc>sstream </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffff>streambuf </td>
            <td bgColor=#ffffff>strstream </td>
        </tr>
    </tbody>
</table>
</p>
<p>　　我们所熟悉的输入输出操作分别是由istream(输入流)和ostream(输出流)这两个类提供的，为了允许双向的输入／输出，由istream和ostream派生出了iostream类。 </p>
<p>　　类的继承关系见下图：<br><img alt="" src="http://www.pconline.com.cn/pcedu/empolder/gj/c/0504/pic/05cppios02.gif"> </p>
<p>iostream库定义了以下三个标准流对象： </p>
<ol>
    <li>cin，表示标准输入(standard input)的istream类对象。cin使我们可以从设备读如数据。
    <li>cout，表示标准输出(standard output)的ostream类对象。cout使我们可以向设备输出或者写数据。
    <li>cerr，表示标准错误(standard error)的osttream类对象。cerr是导出程序错误消息的地方，它只能允许向屏幕设备写数据。 </li>
</ol>
<p>　　输出主要由重载的左移操作符（&lt;&lt;）来完成，输入主要由重载的右移操作符(&gt;&gt;)完成: </p>
<ol>
    <li>&gt;&gt;a表示将数据放入a对象中。
    <li>&lt;&lt;a表示将a对象中存储的数据拿出。 </li>
</ol>
<p>　　这些标准的流对象都有默认的所对应的设备，见下表：<br>
<table cellSpacing=1 cellPadding=0 border=1>
    <tbody>
        <tr>
            <th class=twikiFirstCol bgColor=#99cccc><a title="Sort by this column" href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?sortcol=0&amp;table=2&amp;up=0#sorted_table"><u><font color=#0000ff>C++对象名</font></u></a> </th>
            <th bgColor=#99cccc><a title="Sort by this column" href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?sortcol=1&amp;table=2&amp;up=0#sorted_table"><u><font color=#0000ff>设备名称</font></u></a> </th>
            <th bgColor=#99cccc><a title="Sort by this column" href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?sortcol=2&amp;table=2&amp;up=0#sorted_table"><u><font color=#0000ff>C中标准设备名</font></u></a> </th>
            <th bgColor=#99cccc><a title="Sort by this column" href="http://www.stlchina.org/twiki/bin/view.pl/Main/STLIOStreamIntro?sortcol=3&amp;table=2&amp;up=0#sorted_table"><u><font color=#0000ff>默认含义</font></u></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=#ffffcc>cout </td>
            <td align=middle bgColor=#ffffcc>显示器屏幕 </td>
            <td align=middle bgColor=#ffffcc>stdout </td>
            <td align=middle bgColor=#ffffcc>标准输出 </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>
<p>　　那么原理上Ｃ++有是如何利用cin／cout对象与左移和右移运算符重载来实现输入输出的呢？ </p>
<p>　　下面我们以输出为例，说明其实现原理： </p>
<ol>
    <li>cout是ostream类的对象，因为它所指向的是标准设备（显示器屏幕），所以它在iostream头文件中作为全局对象进行定义。
    <li>ostream cout(stdout);//其默认指向的C中的标准设备名，作为其构造函数的参数使用。
    <li>在iostream.h头文件中，ostream类对应每个基本数据类型都有其友元函数对左移操作符进行了友元函数的重载。
    <ul>
        <li>ostream&amp; operator&lt;&lt;(ostream &amp;temp,int source);
        <li>ostream&amp; operator&lt;&lt;(ostream &amp;temp,char *ps);
        <li>... 等等 </li>
    </ul>
    </li>
</ol>
<p>　　一句输出语句：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;"中国软件开发实验室";这样的语句出现的时候，就能够构成连续输出。 </p>
<p>　　由于iostream库不光支持对象的输入输出，同时也支持文件流的输入输出，所以在详细讲解左移与右移运算符重载只前，我们有必要先对文件的输入输出以及输入输出的控制符有所了解。 </p>
<h3><a name=2_fstream_ifstream_ofstream></a>2 fstream: ifstream 和 ofstream </h3>
<p>　　和文件有关系的输入输出类主要在fstream.h这个头文件中被定义，在这个头文件中主要被定义了三个类，由这三个类控制对文件的各种输入输出操作，他们分别是ifstream、ofstream、fstream，其中fstream类是由iostream类派生而来，他们之间的继承关系见下图所示。<br><img alt="" src="http://www.pconline.com.cn/pcedu/empolder/gj/c/0504/pic/05cppios04.gif"> </p>
<p>由于文件设备并不像显示器屏幕与键盘那样是标准默认设备，所以它在fstream.h头文件中是没有像cout那样预先定义的全局对象，所以我们必须自己定义一个该类的对象，我们要以文件作为设备向文件输出信息(也就是向文件写数据)，那么就应该使用ofstream类。 </p>
<p>　　ofstream类的默认构造函数原形为： </p>
<div class=fragment>
<pre>ofstream::ofstream(<font color=#a52a2a>const</font> <font color=#a52a2a>char</font> *filename,<font color=#a52a2a>int</font> mode = ios::out,<font color=#a52a2a>int</font> openprot = filebuf::openprot);</pre>
<pre>&nbsp;</pre>
</div>
<ul>
    <li>filename：　　要打开的文件名
    <li>mode：　　　　要打开文件的方式
    <li>prot：　　　　打开文件的属性 </li>
</ul>
<p>　　其中mode和openprot这两个参数的可选项表见下表：
<table cellSpacing=1 cellPadding=0 border=1>
    <tbody>
        <tr>
            <td class=twikiFirstCol align=middle bgColor=#ffffcc colSpan=2>mode属性表 </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffff>ios::app </td>
            <td bgColor=#ffffff>以追加的方式打开文件 </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffcc>ios::ate </td>
            <td bgColor=#ffffcc>文件打开后定位到文件尾，ios:app就包含有此属性 </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffff>ios::binary </td>
            <td bgColor=#ffffff>以二进制方式打开文件，缺省的方式是文本方式。两种方式的区别见前文 </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffcc>ios::in </td>
            <td bgColor=#ffffcc>文件以输入方式打开 </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffff>ios::out </td>
            <td bgColor=#ffffff>文件以输出方式打开 </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffcc>ios::trunc </td>
            <td bgColor=#ffffcc>如果文件存在，把文件长度设为0 </td>
        </tr>
    </tbody>
</table>
　　可以用&#8220;或&#8221;把以上属性连接起来，如ios::out|ios::binary。
<table cellSpacing=1 cellPadding=0 border=1>
    <tbody>
        <tr>
            <td class=twikiFirstCol align=middle bgColor=#ffffcc colSpan=2>openprot属性表 </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffff>属性 </td>
            <td align=middle bgColor=#ffffff>含义 </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffcc>0 </td>
            <td bgColor=#ffffcc>普通文件，打开访问 </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffff>1 </td>
            <td bgColor=#ffffff>只读文件 </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffcc>2 </td>
            <td bgColor=#ffffcc>隐含文件 </td>
        </tr>
        <tr>
            <td class=twikiFirstCol bgColor=#ffffff>4 </td>
            <td bgColor=#ffffff>系统文件 </td>
        </tr>
    </tbody>
</table>
　　可以用&#8220;或&#8221;或者&#8220;+&#8221;把以上属性连接起来 ，如3或1|2就是以只读和隐含属性打开文件。 </p>
<p>实例代码如下： </p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;fstream&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std; </pre>
<pre><font color=#a52a2a>int</font> main()  { </pre>
<pre>        ofstream myfile("<font color=#0000ff>c:\\1.txt</font>",ios::out|ios::trunc,0);</pre>
<pre>         myfile&lt;&lt;"<font color=#0000ff>中国软件开发实验室</font>"&lt;&lt;endl&lt;&lt;"<font color=#0000ff>网址：</font>"&lt;&lt;"<font color=#0000ff>www.cndev-lab.com</font>";</pre>
<pre>         myfile.close()；</pre>
<pre>         system("<font color=#0000ff>pause</font>"); }</pre>
<pre>&nbsp;</pre>
</div>
<p>　　文件使用完后可以使用close成员函数关闭文件。 </p>
<p>　　ios::app为追加模式，在使用追加模式的时候同时进行文件状态的判断是一个比较好的习惯。 </p>
<p>　　示例如下： </p>
<p>&nbsp;</p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;iostream&gt;</pre>
<pre> <font color=#000080>#include</font> &lt;fstream&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;</pre>
<pre> <font color=#a52a2a>int</font> main()  {</pre>
<pre>         ofstream myfile("<font color=#0000ff>c:\\1.txt</font>",ios::app,0);</pre>
<pre>         <font color=#a52a2a>if</font>(!myfile)<font color=#008000>//或者写成myfile.fail() </font>        {</pre>
<pre>                 cout&lt;&lt;"<font color=#0000ff>文件打开失败，目标文件状态可能为只读！</font>";</pre>
<pre>                 system("<font color=#0000ff>pause</font>");</pre>
<pre>                 exit(1);         }</pre>
<pre>         myfile&lt;&lt;"<font color=#0000ff>中国软件开发实验室</font>"&lt;&lt;endl&lt;&lt;"<font color=#0000ff>网址：</font>"&lt;&lt;"<font color=#0000ff><a href='http://www.cndev-lab.com"<<endl/'>www.cndev-lab.com</font>"&lt;&lt;endl</a>;</pre>
<pre>         myfile.close(); </pre>
<pre>     }</pre>
<pre>&nbsp;</pre>
</div>
<p>　　在定义ifstream和ofstream类对象的时候，我们也可以不指定文件。以后可以通过成员函数open()显式的把一个文件连接到一个类对象上。 </p>
<p>　　例如： </p>
<p>&nbsp;</p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;iostream&gt; </pre>
<pre><font color=#000080>#include</font> &lt;fstream&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;</pre>
<pre> <font color=#a52a2a>int</font> main()  {</pre>
<pre>         ofstream myfile;</pre>
<pre>         myfile.open("<font color=#0000ff>c:\\1.txt</font>",ios::out|ios::app,0);</pre>
<pre>         <font color=#a52a2a>if</font>(!myfile)<font color=#008000>//或者写成myfile.fail() </font>        </pre>
<pre>           { </pre>
<pre>                cout&lt;&lt;"<font color=#0000ff>文件创建失败,磁盘不可写或者文件为只读!</font>";</pre>
<pre>                system("<font color=#0000ff>pause</font>"); </pre>
<pre>                exit(1);</pre>
<pre>         }         </pre>
<pre>          myfile&lt;&lt;"<font color=#0000ff>中国软件开发实验室</font>"&lt;&lt;endl&lt;&lt;"<font color=#0000ff>网址：</font>"&lt;&lt;"<font color=#0000ff><a href='http://www.cndev-lab.com"<<endl/'>www.cndev-lab.com</font>"&lt;&lt;endl</a>;</pre>
<pre>         myfile.close(); </pre>
<pre>}</pre>
<pre>&nbsp;</pre>
</div>
<p>　　下面我们来看一下是如何利用ifstream类对象，将文件中的数据读取出来，然后再输出到标准设备中的例子。 </p>
<p>　　代码如下： </p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;iostream&gt;</pre>
<pre> <font color=#000080>#include</font> &lt;fstream&gt;</pre>
<pre> <font color=#000080>#include</font> &lt;string&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;</pre>
<pre> <font color=#a52a2a>int</font> main()  {</pre>
<pre>         ifstream myfile;</pre>
<pre>         myfile.open("<font color=#0000ff>c:\\1.txt</font>",ios::in,0);</pre>
<pre>         <font color=#a52a2a>if</font>(!myfile)</pre>
<pre>         { </pre>
<pre>                cout&lt;&lt;"<font color=#0000ff>文件读错误</font>";</pre>
<pre>                 system("<font color=#0000ff>pause</font>");</pre>
<pre>                 exit(1);</pre>
<pre>         }</pre>
<pre>         <font color=#a52a2a>char</font> ch;</pre>
<pre>         string content;</pre>
<pre>         <font color=#a52a2a>while</font>(myfile.get(ch))</pre>
<pre>         {</pre>
<pre>                 content+=ch;</pre>
<pre>                 cout.put(ch);<font color=#008000>//cout&lt;&lt;ch;这么写也是可以的<br> </font>        }        </pre>
<pre>        myfile.close(); </pre>
<pre>        cout&lt;&lt;content;</pre>
<pre>         system("<font color=#0000ff>pause</font>");</pre>
<pre> }</pre>
<pre>&nbsp;</pre>
</div>
<p>　　上例中，我们利用成员函数get()，逐一的读取文件中的有效字符，再利用put()成员函数，将文件中的数据通过循环逐一输出到标准设备(屏幕)上， get()成员函数会在文件读到默尾的时候返回假值，所以我们可以利用它的这个特性作为while循环的终止条件，我们同时也在上例中引入了C++风格的字符串类型string，在循环读取的时候逐一保存到content中，要使用string类型，必须包含string.h的头文件。 </p>
<p>&nbsp;</p>
<p>我们在简单介绍过ofstream类和ifstream类后，我们再来看一下fstream类，fstream类是由iostream派生而来，fstream类对象可以同对文件进行读写操作。 </p>
<p>　　示例代码如下： </p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;iostream&gt;</pre>
<pre> <font color=#000080>#include</font> &lt;fstream&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;</pre>
<pre> <font color=#a52a2a>int</font> main()  {</pre>
<pre>         fstream myfile;</pre>
<pre>         myfile.open("<font color=#0000ff>c:\\1.txt</font>",ios::out|ios::app,0);</pre>
<pre>         <font color=#a52a2a>if</font>(!myfile)</pre>
<pre>         {</pre>
<pre>                 cout&lt;&lt;"<font color=#0000ff>文件写错误,文件属性可能为只读!</font>"&lt;&lt;endl;</pre>
<pre>                 system("<font color=#0000ff>pause</font>");</pre>
<pre>                 exit(1);</pre>
<pre>         }         </pre>
<pre>         myfile&lt;&lt;"<font color=#0000ff>中国软件开发实验室</font>"&lt;&lt;endl&lt;&lt;"<font color=#0000ff>网址：</font>"&lt;&lt;"<font color=#0000ff><a href='http://www.cndev-lab.com"<<endl/'>www.cndev-lab.com</font>"&lt;&lt;endl</a>;</pre>
<pre>        myfile.close();</pre>
<pre>        myfile.open("<font color=#0000ff>c:\\1.txt</font>",ios::in,0);</pre>
<pre>         <font color=#a52a2a>if</font>(!myfile)</pre>
<pre>         { </pre>
<pre>                cout&lt;&lt;"<font color=#0000ff>文件读错误,文件可能丢失!</font>"&lt;&lt;endl;</pre>
<pre>                 system("<font color=#0000ff>pause</font>");</pre>
<pre>                 exit(1);</pre>
<pre>         }         </pre>
<pre>           <font color=#a52a2a>char</font> ch; </pre>
<pre>        <font color=#a52a2a>while</font>(myfile.get(ch))</pre>
<pre>         {</pre>
<pre>                 cout.put(ch);</pre>
<pre>         }</pre>
<pre>         myfile.close();</pre>
<pre>         system("<font color=#0000ff>pause</font>");</pre>
<pre> }</pre>
<pre>&nbsp;</pre>
</div>
<p>　　由于fstream类可以对文件同时进行读写操作，所以对它的对象进行初始话的时候一定要显式的指定mode和openprot参数。 </p>
<p>　　接下来我们来学习一下串流类的基础知识，什么叫串流类？ </p>
<h3><a name=3_strstream_ostrstream_istrstrea></a>3 strstream: ostrstream 和 istrstream </h3>
<p>　　简单的理解就是能够控制字符串类型对象进行输入输出的类，C++不光可以支持C++风格的字符串流控制，还可以支持C风格的字符串流控制。 </p>
<p>　　我们先看看看C++是如何对C风格的字符串流进行控制的，C中的字符串其实也就是字符数组，字符数组内的数据在内存中的位置的排列是连续的，我们通常用 char str[size]或者char *str的方式声明创建C风格字符数组，为了能让字符数组作为设备并提供输入输出操作，C++引入了ostrstream、istrstream、 strstream这三个类，要使用他们创建对象就必须包含strstream.h头文件。 </p>
<ul>
    <li>istrstream类用于执行C风格的串流的输入操作，也就是以字符串数组作为输入设备。
    <li>ostrstream类用于执行C风格的串流的输出操作，也就是一字符串数组作为输出设备。
    <li>strstream类同时可以支持C风格的串流的输入输出操作。 </li>
</ul>
<p>　　istrstream类是从istream（输入流类）和strstreambase（字符串流基类）派生而来，ostrstream是从 ostream（输出流类）和strstreambase（字符串流基类）派生而来，strstream则是从iostream(输入输出流类)和和 strstreambase（字符串流基类）派生而来。 </p>
<p>　　他们的继承关系如下图所示:<br><img alt="" src="http://www.pconline.com.cn/pcedu/empolder/gj/c/0504/pic/05cppios05.gif"> </p>
<p>　　串流同样不是标准设备，不会有预先定义好的全局对象，所以不能直接操作，需要通过构造函数创建对象。 </p>
<p>类istrstream的构造函数原形如下： </p>
<div class=fragment>
<pre>　　istrstream::istrstream(<font color=#a52a2a>const</font> <font color=#a52a2a>char</font> *str,<font color=#a52a2a>int</font> size);</pre>
<pre>&nbsp;</pre>
</div>
<p>　　参数1表示字符串数组,而参数2表示数组大小，当size为0时，表示istrstream类对象直接连接到由str所指向的内存空间并以\0结尾的字符串。 </p>
<p>　　下面的示例代码就是利用istrstream类创建类对象，制定流输入设备为字符串数组，通过它向一个字符型对象输入数据。代码如下： </p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;iostream&gt;</pre>
<pre> <font color=#000080>#include</font> &lt;strstream&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;</pre>
<pre> <font color=#a52a2a>int</font> main()  {</pre>
<pre>         <font color=#a52a2a>char</font> *name = "<font color=#0000ff>www.cndev-lab.com</font>";</pre>
<pre>         <font color=#a52a2a>int</font> arraysize = strlen(name)+1;</pre>
<pre>         istrstream is(name,arraysize);</pre>
<pre>         <font color=#a52a2a>char</font> temp;</pre>
<pre>         is&gt;&gt;temp;</pre>
<pre>         cout&lt;&lt;temp;</pre>
<pre>         system("<font color=#0000ff>pause</font>"); </pre>
<pre>}</pre>
<pre>&nbsp;</pre>
</div>
<p>　　类ostrstream用于执行串流的输出，它的构造函数如下所示： </p>
<div class=fragment>
<pre>　　ostrstream::ostrstream(<font color=#a52a2a>char</font> *_Ptr,<font color=#a52a2a>int</font> streamsize,<font color=#a52a2a>int</font> Mode = ios::out);</pre>
<pre>&nbsp;</pre>
</div>
<p>　　第一个参数是字符数组，第二个是说明数组的大小，第三个参数是指打开方式。 </p>
<p>　　我们来一个示例代码： </p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;iostream&gt;</pre>
<pre> <font color=#000080>#include</font> &lt;strstream&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;</pre>
<pre> <font color=#a52a2a>int</font> main()  {</pre>
<pre>         <font color=#a52a2a>int</font> arraysize=1;</pre>
<pre>         <font color=#a52a2a>char</font> *pbuffer=<font color=#a52a2a>new</font> <font color=#a52a2a>char</font>[arraysize];</pre>
<pre>         ostrstream ostr(pbuffer,arraysize,ios::out);</pre>
<pre>         ostr&lt;&lt;arraysize&lt;&lt;ends;<font color=#008000>//使用ostrstream输出到流对象的时候,要用ends结束字符串 </font>  </pre>
<pre>          cout&lt;&lt;pbuffer;</pre>
<pre>         <font color=#a52a2a>delete</font>[] pbuffer;</pre>
<pre>         system("<font color=#0000ff>pause</font>");</pre>
<pre> }</pre>
<pre>&nbsp;</pre>
</div>
<p>　　上面的代码中，我们创建一个c风格的串流输出对象ostr，我们将arraysize内的数据成功的以字符串的形式输出到了ostr对象所指向的pbuffer指针的堆空间中，pbuffer也正是我们要输出的字符串数组，在结尾要使用ends结束字符串，如果不这么做就有溢出的危险。 </p>
<h3><a name=4_stringstream></a>4 stringstream </h3>
<p>对于stringstream了来说，不用我多说，大家也已经知道它是用于C++风格的字符串的输入输出的。　　stringstream的构造函数原形如下： </p>
<div class=fragment>
<pre>　　stringstream::stringstream(string str);</pre>
<pre>&nbsp;</pre>
</div>
<p>　　示例代码如下: </p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;iostream&gt;</pre>
<pre> <font color=#000080>#include</font> &lt;sstream&gt;</pre>
<pre> <font color=#000080>#include</font> &lt;string&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;</pre>
<pre> <font color=#a52a2a>int</font> main()  { </pre>
<pre>        stringstream ostr("<font color=#0000ff>ccc</font>");</pre>
<pre>         ostr.put('d');</pre>
<pre>         ostr.put('e');</pre>
<pre>         ostr&lt;&lt;"<font color=#0000ff>fg</font>"; </pre>
<pre>        string gstr = ostr.str();</pre>
<pre>         cout&lt;&lt;gstr&lt;&lt;endl;</pre>
<pre>                 <font color=#a52a2a>char</font> a;</pre>
<pre>         ostr&gt;&gt;a;         cout&lt;&lt;a;                 system("<font color=#0000ff>pause</font>"); }</pre>
<pre>&nbsp;</pre>
</div>
<p>　　除此而外，stringstream类的对象我们还常用它进行string与各种内置类型数据之间的转换。示例代码如下： </p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;iostream&gt;</pre>
<pre> <font color=#000080>#include</font> &lt;sstream&gt;</pre>
<pre> <font color=#000080>#include</font> &lt;string&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;</pre>
<pre> <font color=#a52a2a>int</font> main()  {</pre>
<pre>         stringstream sstr;         <font color=#008000>//--------int转string----------- </font>  </pre>
<pre>        <font color=#a52a2a>int</font> a=100;</pre>
<pre>         string str;</pre>
<pre>         sstr&lt;&lt;a; </pre>
<pre>        sstr&gt;&gt;str; </pre>
<pre>        cout&lt;&lt;str&lt;&lt;endl;         <font color=#008000>//--------string转char[]-------- </font>   </pre>
<pre>sstr.clear();<font color=#008000>//如果你想通过使用同一stringstream对象实现多种类型的转换，<br>             // 请注意在每一次转换之后都必须调用clear()成员函数。 </font>    </pre>
<pre>       string name = "<font color=#0000ff>colinguan</font>";</pre>
<pre>         <font color=#a52a2a>char</font> cname[200]; </pre>
<pre>        sstr&lt;&lt;name; </pre>
<pre>        sstr&gt;&gt;cname;</pre>
<pre>         cout&lt;&lt;cname; </pre>
<pre>        system("<font color=#0000ff>pause</font>");</pre>
<pre> }</pre>
<pre>&nbsp;</pre>
</div>
<p>　　接下来我们来学习一下输入/输出的状态标志的相关知识. </p>
<h3><a name=5_io_state></a><a name=5_io_state_></a>5 io_state 输入/输出的状态标志 </h3>
<p>C++中负责的输入/输出的系统包括了关于每一个输入/输出操作的结果的记录信息。这些当前的状态信息被包含在io_state类型的对象中。io_state是一个枚举类型（就像open_mode一样），以下便是它包含的值。 </p>
<ul>
    <li>goodbit 无错误
    <li>Eofbit 已到达文件尾
    <li>failbit 非致命的输入/输出错误，可挽回
    <li>badbit　致命的输入/输出错误,无法挽回 </li>
</ul>
<p>有两种方法可以获得输入/输出的状态信息。一种方法是通过调用rdstate()函数，它将返回当前状态的错误标记。例如，假如没有任何错误，则rdstate()会返回goodbit.下例示例，表示出了rdstate()的用法： </p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;iostream&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;</pre>
<pre> <font color=#a52a2a>int</font> main()  {</pre>
<pre>         <font color=#a52a2a>int</font> a;</pre>
<pre>         cin&gt;&gt;a;</pre>
<pre>         cout&lt;&lt;cin.rdstate()&lt;&lt;endl;</pre>
<pre>         <font color=#a52a2a>if</font>(cin.rdstate() == ios::goodbit)</pre>
<pre>         {             cout&lt;&lt;"<font color=#0000ff>输入数据的类型正确，无错误！</font>"&lt;&lt;endl;         }</pre>
<pre>         <font color=#a52a2a>if</font>(cin.rdstate() == ios_base::failbit)</pre>
<pre>         {             cout&lt;&lt;"<font color=#0000ff>输入数据类型错误，非致命错误，可清除输入缓冲区挽回！</font>"&lt;&lt;endl;       } </pre>
<pre>        system("<font color=#0000ff>pause</font>"); </pre>
<pre>}</pre>
<pre>&nbsp;</pre>
</div>
<p>　　另一种方法则是使用下面任何一个函数来检测相应的输入/输出状态： </p>
<div class=fragment>
<pre><font color=#a52a2a>bool</font> bad();<font color=#a52a2a>bool</font> eof();<font color=#a52a2a>bool</font> fail();<font color=#a52a2a>bool</font> good();</pre>
<pre>&nbsp;</pre>
</div>
<p>　　下例示例，表示出了上面各成员函数的用法： </p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;iostream&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;</pre>
<pre> <font color=#a52a2a>int</font> main()  {</pre>
<pre>         <font color=#a52a2a>int</font> a;</pre>
<pre>         cin&gt;&gt;a;</pre>
<pre>         cout&lt;&lt;cin.rdstate()&lt;&lt;endl;</pre>
<pre>         <font color=#a52a2a>if</font>(cin.good())</pre>
<pre>         {                 cout&lt;&lt;"<font color=#0000ff>输入数据的类型正确，无错误！</font>"&lt;&lt;endl;         }</pre>
<pre>         <font color=#a52a2a>if</font>(cin.fail())</pre>
<pre>         {                 cout&lt;&lt;"<font color=#0000ff>输入数据类型错误，非致命错误，可清除输入缓冲区挽回！</font>"&lt;&lt;endl;    }</pre>
<pre>         system("<font color=#0000ff>pause</font>"); </pre>
<pre>}</pre>
<pre>&nbsp;</pre>
</div>
<p>　　如果错误发生，那么流状态既被标记为错误，你必须清除这些错误状态，以使你的程序能正确适当地继续运行。要清除错误状态，需使用clear()函数。此函数带一个参数，它是你将要设为当前状态的标志值。，只要将ios::goodbit作为实参。 </p>
<p>　　示例代码如下： </p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;iostream&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;</pre>
<pre> <font color=#a52a2a>int</font> main()</pre>
<pre>  {         <font color=#a52a2a>int</font> a;</pre>
<pre>         cin&gt;&gt;a; </pre>
<pre>        cout&lt;&lt;cin.rdstate()&lt;&lt;endl;</pre>
<pre>         cin.clear(ios::goodbit);</pre>
<pre>         cout&lt;&lt;cin.rdstate()&lt;&lt;endl; </pre>
<pre>        system("<font color=#0000ff>pause</font>");</pre>
<pre> }</pre>
<pre>&nbsp;</pre>
</div>
<p>通常当我们发现输入有错又需要改正的时候，使用clear()更改标记为正确后，同时也需要使用get()成员函数清除输入缓冲区，以达到重复输入的目的。 </p>
<p>　　示例代码如下： </p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;iostream&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;</pre>
<pre> <font color=#a52a2a>int</font> main()  {</pre>
<pre>         <font color=#a52a2a>int</font> a;</pre>
<pre>         <font color=#a52a2a>while</font>(1)</pre>
<pre>         { </pre>
<pre>                cin&gt;&gt;a;</pre>
<pre>                 <font color=#a52a2a>if</font>(!cin)<font color=#008000>//条件可改写为cin.fail() </font>    </pre>
<pre>                   { </pre>
<pre>                        cout&lt;&lt;"<font color=#0000ff>输入有错!请重新输入</font>"&lt;&lt;endl;</pre>
<pre>                         cin.clear();</pre>
<pre>                         cin.get(); </pre>
<pre>                }   </pre>
<pre>              <font color=#a52a2a>else</font>  </pre>
<pre>               {    </pre>
<pre>                     cout&lt;&lt;a;</pre>
<pre>                         <font color=#a52a2a>break</font>; </pre>
<pre>                }  </pre>
<pre>       }        </pre>
<pre> system("<font color=#0000ff>pause</font>"); </pre>
<pre>}</pre>
<pre>&nbsp;</pre>
</div>
<p>　　最后再给出一个对文件流错误标记处理的例子，巩固学习，代码如下： </p>
<div class=fragment>
<pre><font color=#000080>#include</font> &lt;iostream&gt;</pre>
<pre> <font color=#000080>#include</font> &lt;fstream&gt; <font color=#a52a2a>using</font> <font color=#a52a2a>namespace</font> std;</pre>
<pre> <font color=#a52a2a>int</font> main()</pre>
<pre>  {         ifstream myfile("<font color=#0000ff>c:\\1.txt</font>",ios_base::in,0);</pre>
<pre>         <font color=#a52a2a>if</font>(myfile.fail())</pre>
<pre>         {                 cout&lt;&lt;"<font color=#0000ff>文件读取失败或指定文件不存在!</font>"&lt;&lt;endl;         }</pre>
<pre>         <font color=#a52a2a>else</font> </pre>
<pre>        { </pre>
<pre>                <font color=#a52a2a>char</font> ch;</pre>
<pre>                 <font color=#a52a2a>while</font>(myfile.get(ch)) </pre>
<pre>                {                         cout&lt;&lt;ch;                 }</pre>
<pre>                 <font color=#a52a2a>if</font>(myfile.eof()) </pre>
<pre>                {                         cout&lt;&lt;"<font color=#0000ff>文件内容已经全部读完</font>"&lt;&lt;endl;            }</pre>
<pre>                 <font color=#a52a2a>while</font>(myfile.get(ch))</pre>
<pre>                 {                         cout&lt;&lt;ch;                 } </pre>
<pre>        } </pre>
<pre>        system("<font color=#0000ff>pause</font>"); </pre>
<pre>}</pre>
<pre><a href="http://www.programfan.com/club/old_showbbs.asp?id=82037"><u><font color=#0000ff>http://www.programfan.com/club/old_showbbs.asp?id=82037</font></u></a></pre>
<pre>//=======================================================================<br>// dbl2str.cpp <br>//=======================================================================<br>// double-to-string test<br>// answer for:<br>//&nbsp;&nbsp;<a href="http://www.programfan.com/club/showbbs.asp?id=81982" target=_blank><u><font color=#0000ff>C++中如何将double转换成string</font></u></a><br>//<br>// meteor135 smith_135@163.com<br>// platform: MSVC 6.0 + MSXP<br>// 2005-6-5 21:38:14<br>//=======================================================================<br><br>#include &lt;iostream&gt;&nbsp;&nbsp;// for printf, sprintf, cout, etc.<br>#include &lt;strstream&gt; // for c-type string: sequential characters<br>#include &lt;sstream&gt;&nbsp;&nbsp;&nbsp;// for c++ string class<br><br>using namespace std;<br><br>int main(int argc, char* argv[])<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;double a = 987.6543;<br>&nbsp;&nbsp;&nbsp;&nbsp;char buf[50];<br>&nbsp;&nbsp;&nbsp;&nbsp;//=======================================================================<br>&nbsp;&nbsp;&nbsp;&nbsp;// in &lt;stdio.h&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;// 2005-6-5 21:26:27<br>&nbsp;&nbsp;&nbsp;&nbsp;//=======================================================================<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("--------------------------------------------------------------\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double-to-string test&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("--------------------------------------------------------------\n\n");<br><br>&nbsp;&nbsp;&nbsp;&nbsp;printf("using sprintf:\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("==============================================================\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("printf(\"a = %%f\",a):\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("a = %f",a);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("\n\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("printf(\"a= %%g\",a):\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("a = %g",a);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("\n\n");<br><br>&nbsp;&nbsp;&nbsp;&nbsp;printf("sprintf(buf,\"%%f\",a) and printf(\"a = %%s\",buf):\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;sprintf(buf,"%f",a);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("a = %s",buf);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("\n\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("sprintf(buf,\"%%g\",a) and printf(\"a = %%s\",buf):\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;sprintf(buf,"%g",a);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("a = %s",buf);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("\n==============================================================\n\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;//===================================================================<br>&nbsp;&nbsp;&nbsp;&nbsp;// in &lt;strstream&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;// 2005-6-5 21:02:05<br>&nbsp;&nbsp;&nbsp;&nbsp;//===================================================================<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"using strstream:"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("==============================================================\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;strstream my_strstream;<br>&nbsp;&nbsp;&nbsp;&nbsp;const char * my_c_string_ptr1;<br>&nbsp;&nbsp;&nbsp;&nbsp;my_strstream&lt;&lt;a&lt;&lt;'\0'; // need '\n'<br>&nbsp;&nbsp;&nbsp;&nbsp;my_c_string_ptr1 = my_strstream.str();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"a = "&lt;&lt;my_c_string_ptr1&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("a = %s",my_c_string_ptr1);<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("\n==============================================================\n\n");<br><br>&nbsp;&nbsp;&nbsp;&nbsp;//=======================================================================<br>&nbsp;&nbsp;&nbsp;&nbsp;// in &lt;sstream&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;// 2005-6-5 21:01:30<br>&nbsp;&nbsp;&nbsp;&nbsp;//=======================================================================<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"using stringstream:"&lt;&lt;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("==============================================================\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;stringstream my_stringstream;<br>&nbsp;&nbsp;&nbsp;&nbsp;string my_string;<br>&nbsp;&nbsp;&nbsp;&nbsp;const char *my_c_string_ptr2;<br>&nbsp;&nbsp;&nbsp;&nbsp;my_stringstream&lt;&lt;a; // no need '\0';<br>&nbsp;&nbsp;&nbsp;&nbsp;my_string = my_stringstream.str();<br><br>&nbsp;&nbsp;&nbsp;&nbsp;// string object<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"a = "&lt;&lt;my_string&lt;&lt;endl;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;// c string pointer<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"a = "&lt;&lt;my_string.c_str()&lt;&lt;endl;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;my_c_string_ptr2 = my_string.c_str();<br>&nbsp;&nbsp;&nbsp;&nbsp;// c string pointer<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("a = %s\n",my_c_string_ptr2);<br>&nbsp;&nbsp;&nbsp;&nbsp;// c string pointer<br>&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"a = "&lt;&lt;my_c_string_ptr2;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;printf("\n==============================================================\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;printf("\nEND\n");<br>&nbsp;&nbsp;&nbsp;&nbsp;return 0;<br>}<br><br>//=======================================================================<br>// output<br>//=======================================================================<br><br>/*<br>--------------------------------------------------------------<br>|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;double-to-string test&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br>--------------------------------------------------------------<br><br>using sprintf:<br>==============================================================<br>printf("a = %f",a):<br>a = 987.654300<br><br>printf("a= %g",a):<br>a = 987.654<br><br>sprintf(buf,"%f",a) and printf("a = %s",buf):<br>a = 987.654300<br><br>sprintf(buf,"%g",a) and printf("a = %s",buf):<br>a = 987.654<br>==============================================================<br><br>using strstream:<br>==============================================================<br>a = 987.654<br>a = 987.654<br>==============================================================<br><br>using stringstream:<br>==============================================================<br>a = 987.654<br>a = 987.654<br>a = 987.654<br>a = 987.654<br>==============================================================<br><br>END<br>*/ </pre>
</div>
<img src ="http://www.cppblog.com/snowell/aggbug/30996.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/snowell/" target="_blank">snowell</a> 2007-08-28 10:46 <a href="http://www.cppblog.com/snowell/articles/30996.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>