📄 right7-2.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"> 第 2 节 </span>><span style="background-color: #CCFF99">
7.2 </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>I/O<font LANG="ZH-CN">中非常重要的一个内容,它的输入是指从磁盘文件流向内存,输出是指从内存流向磁盘。</font>C++<font LANG="ZH-CN">中提供了三个文件流类:</font>ofstream<font LANG="ZH-CN">,</font>ifstream<font LANG="ZH-CN">,</font>fstream<font LANG="ZH-CN">,定义在</font>fstream.h<font LANG="ZH-CN">头文件中。其中</font>fstream<font LANG="ZH-CN">是</font>ofstream<font LANG="ZH-CN">和</font>ifstream<font LANG="ZH-CN">多重继承的子类。各文件流类的功能如下:</font></font></p>
<font SIZE="3">
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font LANG="ZH-CN">①
</font>ofstream<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>ifstream<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>fstream<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">1.
文件流构造函数</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>ofstream<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">oftream::ofstream(char
* pFileName, int mode=ios::out, int prot=filebuf::openprot);</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">第一个参数是指文件路径及名称字符串,第二个参数说明文件打开方式,第三个参数说明文件保护方式。打开方式见<a href="table7-1.htm" target="_blank">表</a></font><a href="table7-1.htm" target="_blank">7-1</a><font LANG="ZH-CN">,保护方式见<a href="table7-2.htm" target="_blank">表</a></font><a href="table7-2.htm" target="_blank">7-2</a><font LANG="ZH-CN">。</font></font></p>
<p ALIGN="CENTER" 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"><a href="example7-6.htm" target="_blank">【例</a></font><a href="example7-6.htm" target="_blank">7-6<font LANG="ZH-CN">】</font></a><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"><a href="example7-7.htm" target="_blank">【例</a></font><a href="example7-7.htm" target="_blank">7-7<font LANG="ZH-CN">】</font></a><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 LANG="ZH-CN" size="2">2.
文件的打开</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>open(
)<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">ofstream
outfile;</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">outfile.open("outfile",
iosmode);</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" color="#008000">ofstream
outfile("outfile", iosmode);</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>7-1<font LANG="ZH-CN">,它们可以用逐位或“</font>|<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">ofstream
outfile("outfile", ios::out|ios::noreplace);</font></p>
</blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font LANG="ZH-CN">当文件打开失败时,文件流对象的</font>bad<font LANG="ZH-CN">标志会置位。可以使用</font>ios<font LANG="ZH-CN">类成员函数</font>clear<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 LANG="ZH-CN" size="2">3.
文件操作</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>iostream<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 LANG="ZH-CN" size="2">4.
关闭文件</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>close(
)<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">outfile.close(
);</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>
<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <span style="position: absolute; left: 33; top: 880"><a href="right7-1.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>
<span style="position: absolute; left: 502; top: 886"><a href="right7-3.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 + -