📄 right7-1.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"><font size="2"><img border="0" src="1.gif" width="63" height="70"></font></span></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"> </font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"><font size="2">
<span style="background-color: #CCFF99">第 7 章 </span>><span style="background-color: #CCFF99"> 第 1 节 </span>><span style="background-color: #CCFF99">
7.1 I/O标准流类 </span></font></p>
<hr color="#008000" size="1">
<p style="line-height: 150%; text-indent: 0; margin: 0" class="右标题" align="left"><font size="2"><font LANG="ZH-CN">在</font>C++<font LANG="ZH-CN">中,输入输出功能是通过</font>I/O<font LANG="ZH-CN">流实现的,</font>I/O<font LANG="ZH-CN">流不是</font>C++<font LANG="ZH-CN">语言的一部分,而是标准</font>C++<font LANG="ZH-CN">库的一部分,是</font>C++<font LANG="ZH-CN">类是一个集合。</font>C++<font LANG="ZH-CN">将输入输出封装为流类,这样既可以使输入输出简单化,而且方便了输入输出功能的扩充,增强输入输出的能力。</font></font></p>
<p style="line-height: 200%; text-indent: 0; margin: 0" align="left"><font size="2">1.
<font LANG="ZH-CN">输入输出流类层次</font></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"><font size="2">
<img border="0" src="right71.gif" width="503" height="182"></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" class="右标题" align="left"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="justify"><font size="2"><font LANG="ZH-CN">ios类作为流基类,由它可以派生出输入流类、输出流类及输入输出流类。
streambuf类提供抽象流缓冲区基类。</font>streambuf<font LANG="ZH-CN">类和</font>ios<font LANG="ZH-CN">类没有继承关系,它们是组合使用的,</font>streambuf<font LANG="ZH-CN">对象作为</font>ios<font LANG="ZH-CN">类的成员出现。事实上,</font>ios<font LANG="ZH-CN">类提供编程界面和格式特征,而</font>streambuf<font LANG="ZH-CN">做实质性的工作。</font></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"> </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>
<font SIZE="3">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0" align="justify"></font><font size="2"><font LANG="ZH-CN">输入输出流类在</font>iostream.h<font LANG="ZH-CN">中预定义了四个全局的流对象:</font>cout<font LANG="ZH-CN">、</font>cerr<font LANG="ZH-CN">、</font>clog<font LANG="ZH-CN">和</font>cin<font LANG="ZH-CN">,用于标准输出和输入。</font>cout<font LANG="ZH-CN">流对象控制向标准输出设备(显示器)的输出,是</font>ostream<font LANG="ZH-CN">类的对象;</font>cin<font LANG="ZH-CN">控制从标准输入设备(键盘)的输入,是</font>istream<font LANG="ZH-CN">类的对象。</font>cerr<font LANG="ZH-CN">与标准错误设备连在一起,是</font>ostream<font LANG="ZH-CN">类的对象,是非缓冲输出,也就是说插入到</font>cerr<font LANG="ZH-CN">的数据会被立刻显示出来,非缓冲输出可以迅速把出错信息告知用户。</font>clog<font LANG="ZH-CN">也是与标准错误设备连在一起的,是</font>ostream<font LANG="ZH-CN">类的对象,但它是缓冲输出。</font></font></p>
<p 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 LANG="ZH-CN" size="2">(1)
输入输出机制</font></p>
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">C++流类提供了许多控制输入输出的成员函数,虽然利用成员函数能实现输入、输出,但非常不方便,为此C++<font LANG="ZH-CN">中通过重载</font>>><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"><font LANG="ZH-CN">重载的“</font>>><font LANG="ZH-CN">”的功能是从输入流中提取数据赋值给一个变量,因此称为提取操作符。而重载的“</font><<<font LANG="ZH-CN">”的功能是将变量中的数据插入到输入流中,因此称为插入操作符。</font></font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(2)
标准输出</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">cout<font LANG="ZH-CN">流对象用于标准输出,请看<a href="example7-1.htm" target="_blank">例</a></font><a href="example7-1.htm" target="_blank">7-1</a><font LANG="ZH-CN">。</font></font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">(3)
<font LANG="ZH-CN">标准输入</font></font></p>
<blockquote>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">cin<font LANG="ZH-CN">流对象用于标准输入,请看<a href="example7-2.htm" target="_blank">例</a></font><a href="example7-1.htm" target="_blank">7-2</a><font LANG="ZH-CN">。</font></font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font LANG="ZH-CN" size="2">(4)常用输入输出成员函数函数</font></p>
<font SIZE="3">
<blockquote>
<p ALIGN="justify" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">①</font>
precision( )<font FACE="??ì?,SimSun" LANG="ZH-CN">:设置显示小数精度,用于</font>cout<font FACE="??ì?,SimSun" 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 FACE="??ì?,SimSun" LANG="ZH-CN">②</font>
width( )<font FACE="??ì?,SimSun" LANG="ZH-CN">:设置域宽,用于</font>cout<font FACE="??ì?,SimSun" 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 FACE="??ì?,SimSun" LANG="ZH-CN">③</font>
flags( )<font FACE="??ì?,SimSun" LANG="ZH-CN">:设置基数和填充字符,用于</font>cout<font FACE="??ì?,SimSun" 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 FACE="??ì?,SimSun" LANG="ZH-CN">④</font>
get( )<font FACE="??ì?,SimSun" LANG="ZH-CN">:读取一个字符,用于</font>cin<font FACE="??ì?,SimSun" 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 FACE="??ì?,SimSun" LANG="ZH-CN">⑤</font>
getline( )<font FACE="??ì?,SimSun" LANG="ZH-CN">:读取一行字符,用于</font>cin<font FACE="??ì?,SimSun" 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 FACE="??ì?,SimSun" LANG="ZH-CN">⑥</font>
read( )<font FACE="??ì?,SimSun" LANG="ZH-CN">:读取一个字符串,用于</font>cin<font FACE="??ì?,SimSun" 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 FACE="??ì?,SimSun" LANG="ZH-CN">⑦</font>
put( )<font FACE="??ì?,SimSun" LANG="ZH-CN">:输出一个字符,用于</font>cout<font FACE="??ì?,SimSun" 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 FACE="??ì?,SimSun" LANG="ZH-CN">⑧</font>
write( )<font FACE="??ì?,SimSun" LANG="ZH-CN">:输出一个字符串,用于</font>cout<font FACE="??ì?,SimSun" LANG="ZH-CN">。</font></font></p>
</blockquote>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN"><a href="example7-3.htm" target="_blank">【例</a></font><a href="example7-3.htm" target="_blank">7-3<font LANG="ZH-CN">】</font></a><font LANG="ZH-CN">将用户的输入转换为大写字母并显示到屏幕上,输入字母“</font>y<font LANG="ZH-CN">”时,输出“</font>OK<font LANG="ZH-CN">”并结束。</font></font></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN"><a href="example7-4.htm" target="_blank">【例</a></font><a href="example7-4.htm" target="_blank">7-4<font LANG="ZH-CN">】</font></a><font LANG="ZH-CN">将英文字母及对应</font>ASCII<font LANG="ZH-CN">码输出到屏幕上。</font></font></p>
<p 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 LANG="ZH-CN" size="2">(5)格式控制符</font></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">
设置</font>I/O<font LANG="ZH-CN">格式除了可以使用</font>ios<font LANG="ZH-CN">类的成员函数如</font>precision(
)<font LANG="ZH-CN">、</font>width( )<font LANG="ZH-CN">、</font>flags( )<font LANG="ZH-CN">外,还可以使用格式控制符。与使用成员函数不同之处在于,格式控制符可以直接嵌入到输入输出语句中。</font></font></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN"><a href="example7-5.htm" target="_blank">【例</a></font><a href="example7-5.htm" target="_blank">7-5<font LANG="ZH-CN">】</font></a><font LANG="ZH-CN">将例</font>7-4<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"><font size="2"> <span style="position: absolute; left: 32; top: 1108"><a href="right6-3.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>
<span style="position: absolute; left: 501; top: 1112"><a href="right7-2.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></font></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -