📄 right7-3.htm
字号:
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>第 7 章</title>
<base target="rbottom">
<style>
<!--
.右标题 { font-size: 10pt; color: #000080; text-indent: 0; margin: 0 }
.右内容 { font-size: 10pt; text-align: left; text-indent: 0; line-height: 100%; margin:
0 }
-->
</style>
</head>
<body>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><span style="position: absolute; left: 4; top: -8"><img border="0" src="1.gif" width="63" height="70"></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left">
<span style="background-color: #CCFF99">第 7 章</span><span style="background-color: #CCFF99">
</span>><span style="background-color: #CCFF99"> 第 </span><span style="background-color: #CCFF99">3
节 </span>><span style="background-color: #CCFF99"> 7.3 </span><span style="background-color: #CCFF99">字节流</span></p>
<hr color="#008000" size="1">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">字节流可直接与内存而不是与文件或标准输入</font><font LANG="ZH-CN">输出一起工作。我们可以用与标准输入</font><font LANG="ZH-CN">输出同样的格式,操作内存里的数据(字节)。如果我们想把数据放入字节流,可以建立一个</font>ostrstream<font LANG="ZH-CN">对象;如果想从字节流中提取数据,就建立一个</font>istrstream<font LANG="ZH-CN">对象。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">1.
<font LANG="ZH-CN">输入流</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">istrstream<font LANG="ZH-CN">类支持一个字符数组作为源的输入流。在构造</font>istrstream<font LANG="ZH-CN">对象前,必须存在一个字符数组,而且这个数组中已经填充了我们想要提取的字符。下面是两个构造函数的原型:</font></font></p>
<blockquote>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">istrstream::istrstream(char*
buf);</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">istrstream::istrstream(char*
buf, int size);</font></p>
</blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">第一个构造函数取一个指向以</font>"\0"<font LANG="ZH-CN">作为结尾符的字符数组的指针,可以提取字节直至遇到</font>"\0"<font LANG="ZH-CN">为止。第二个构造函数还需要这个数组的大小,但不需要数组包含字符串的结尾符</font>"\0"<font LANG="ZH-CN">,可以一直提取字节到</font>buf[size-1]<font LANG="ZH-CN">,而不管是否遇到</font>"\0"<font LANG="ZH-CN">。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">2.
<font LANG="ZH-CN">输出流</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">ostrstream<font LANG="ZH-CN">类支持一个字符数组作为数据传输目的地的输出流,它可以使用为它申请的存储空间,这时字节在内存中被格式化;也可以使用自动分配的存储空间。</font></font></p>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">为</font>ostrstream<font LANG="ZH-CN">申请存储空间的方法是通过</font>ostrstream<font LANG="ZH-CN">有参的构造函数:</font></font></p>
<blockquote>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">ostrstream(char*,
int, int=ios::out);</font></p>
</blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">第一个参数是缓冲区的指针,第二个参数是缓冲区的大小,第三个参数是打开模式。如果是缺省的模式,则从缓冲区头部开始添加新的字符;如果打开模式是</font>ios::ate<font LANG="ZH-CN">或</font>ios::app<font LANG="ZH-CN">,则从缓冲区中的字符串的结尾符处开始添加新的字符(结尾符不后移,只是被简单地覆盖,下面程序中</font>os<<ends<font LANG="ZH-CN">的作用就是在</font>buf<font LANG="ZH-CN">后面加上结尾符)。</font></font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">【例</font>7-8<font LANG="ZH-CN">】输出字节流。</font></font></p>
<blockquote>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">#include
"strstrea.h"</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">void
main( )</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">{</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">int
n;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">float
f;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">char
buf[80];</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">ostrstream
os(buf,80);</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">cin>>n>>f;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">os<<"n="<<n<<endl;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">os<<"f="<<f<<endl;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">os<<ends;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">cout<<buf;</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">}</font></p>
</blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">程序运行结果是:</font></p>
<blockquote>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">n=3</font></p>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">f=0.14159</font></p>
</blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font LANG="ZH-CN" size="2">如果使用自动分配存储空间的方法,则使用无参的构造函数:</font></p>
<blockquote>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">ostrstream
os;</font></p>
</blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">这时对象</font>os<font LANG="ZH-CN">在堆中分配一块存储空间,并自己维护。当用户输入不断增加,原存储块不够用时,它可以再分配更多的存储空间。用户可以通过调用成员函数</font>rdbuf(
)<font LANG="ZH-CN">返回缓冲区指针,以访问缓冲区的内容。</font></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"> </p>
<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <span style="position: absolute; left: 32; top: 951"><a href="right7-2.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>
<span style="position: absolute; left: 502; top: 949"><a href="right7-4.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -