﻿<?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++博客-KaneCpp-文章分类-Cpp</title><link>http://www.cppblog.com/KaneCpp/category/20128.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 22 Oct 2012 04:02:59 GMT</lastBuildDate><pubDate>Mon, 22 Oct 2012 04:02:59 GMT</pubDate><ttl>60</ttl><item><title>fstream的使用方法介绍 (转)</title><link>http://www.cppblog.com/KaneCpp/articles/193664.html</link><dc:creator>Kane</dc:creator><author>Kane</author><pubDate>Mon, 22 Oct 2012 04:02:00 GMT</pubDate><guid>http://www.cppblog.com/KaneCpp/articles/193664.html</guid><wfw:comment>http://www.cppblog.com/KaneCpp/comments/193664.html</wfw:comment><comments>http://www.cppblog.com/KaneCpp/articles/193664.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/KaneCpp/comments/commentRss/193664.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/KaneCpp/services/trackbacks/193664.html</trackback:ping><description><![CDATA[<div><div> 		<div> <div> <p>在C++中，有一个stream这个类，所有的I/O都以这个&#8220;流&#8221;类为基础的，包括我们要认识的文件I/O，stream这个类有两个重要的运算符：</p> <p>1、插入器(&lt;&lt;)<br /> 向流输出数据。比如说系统有一个默认的标准输出流(cout)，一般情况下就是指的显示器，所以，cout&lt;&lt;"Write Stdout"&lt;&lt;' ';就表示把字符串"Write Stdout"和换行字符(' ')输出到标准输出流。</p> <p>2、析取器(&gt;&gt;)<br /> 从流中输入数据。比如说系统有一个默认的标准输入流(cin)，一般情况下就是指的键盘，所以，cin&gt;&gt;x;就表示从标准输入流中<a name="1"></a><strong style="color: black; background-color: #a0ffff">读取</strong>一个指定类型(即变量x的类型)的数据。</p> <p>　　在C++中，对文件的操作是通过stream的子类<a name="0"></a><strong style="color: black; background-color: #ffff66">fstream</strong>(file stream)来实现的，所以，要用这种方式操作文件，就必须加入头文件<strong style="color: black; background-color: #ffff66">fstream</strong>.h。下面就把此类的文件操作过程一一道来。</p> <p>一、打开文件<br /> 在<strong style="color: black; background-color: #ffff66">fstream</strong>类中，有一个成员函数open()，就是用来打开文件的，其原型是：</p> <p>void open(const char* filename,int mode,int access);</p> <p>参数：</p> <p>filename：　　要打开的文件名 <br /> mode：　　　　要打开文件的方式 <br /> access：　　　打开文件的属性<br /> 打开文件的方式在类ios(是所有流式I/O类的基类)中定义，常用的值如下： </p> <p>ios::app：　　　以追加的方式打开文件 <br /> ios::ate：　　　文件打开后定位到文件尾，ios:app就包含有此属性 <br /> ios::binary： 　以<a name="2"></a><strong style="color: black; background-color: #99ff99">二进制</strong>方式打开文件，缺省的方式是文本方式。两种方式的区别见前文 <br /> ios::in：　　　 文件以输入方式打开 <br /> ios::out：　　　文件以输出方式打开 <br /> ios::nocreate： 不建立文件，所以文件不存在时打开失败　 <br /> ios::noreplace：不覆盖文件，所以打开文件时如果文件存在失败 <br /> ios::trunc：　　如果文件存在，把文件长度设为0 <br /> 可以用&#8220;或&#8221;把以上属性连接起来，如ios::out|ios::binary</p> <p>　　打开文件的属性取值是：</p> <p>0：普通文件，打开访问 <br /> 1：只读文件 <br /> 2：隐含文件 <br /> 4：系统文件 <br /> 可以用&#8220;或&#8221;或者&#8220;+&#8221;把以上属性连接起来 ，如3或1|2就是以只读和隐含属性打开文件。</p> <p>　　例如：以<strong style="color: black; background-color: #99ff99">二进制</strong>输入方式打开文件c:config.sys </p> <p>　　<strong style="color: black; background-color: #ffff66">fstream</strong> file1;<br /> file1.open("c:\config.sys",ios::binary|ios::in,0);</p> <p>　　如果open函数只有文件名一个参数，则是以读/写普通文件打开，即：</p> <p>　　file1.open("c:\config.sys");&lt;=&gt;file1.open("c:\config.sys",ios::in|ios::out,0);</p> <p>　　另外，<strong style="color: black; background-color: #ffff66">fstream</strong>还有和open()一样的构造函数，对于上例，在定义的时侯就可以打开文件了：</p> <p>　　<strong style="color: black; background-color: #ffff66">fstream</strong> file1("c:\config.sys");</p> <p>　　特别提出的是，<strong style="color: black; background-color: #ffff66">fstream</strong>有两个子类：ifstream(input file stream)和ofstream(outpu file stream)，ifstream默认以输入方式打开文件，而ofstream默认以输出方式打开文件。</p> <p>　　ifstream file2("c:\pdos.def");//以输入方式打开文件<br /> ofstream file3("c:\x.123");//以输出方式打开文件</p> <p>　　所以，在实际应用中，根据需要的不同，选择不同的类来定义：如果想以输入方式打开，就用ifstream来定义；如果想以输出方式打开，就用ofstream来定义；如果想以输入/输出方式来打开，就用<strong style="color: black; background-color: #ffff66">fstream</strong>来定义。</p> <p>二、关闭文件<br /> 打开的文件使用完成后一定要关闭，<strong style="color: black; background-color: #ffff66">fstream</strong>提供了成员函数close()来完成此操作，如：file1.close();就把file1相连的文件关闭。</p> <p>三、读写文件<br /> 读写文件分为文本文件和<strong style="color: black; background-color: #99ff99">二进制</strong>文件的<strong style="color: black; background-color: #a0ffff">读取</strong>，对于文本文件的<strong style="color: black; background-color: #a0ffff">读取</strong>比较简单，用插入器和析取器就可以了；而对于<strong style="color: black; background-color: #99ff99">二进制</strong>的<strong style="color: black; background-color: #a0ffff">读取</strong>就要复杂些，下要就详细的介绍这两种方式</p> <p>　　1、文本文件的读写<br /> 文本文件的读写很简单：用插入器(&lt;&lt;)向文件输出；用析取器(&gt;&gt;)从文件输入。假设file1是以输入方式打开，file2以输出打开。示例如下：</p> <p>　　file2&lt;&lt;"I Love You";//向文件写入字符串"I Love You"<br /> int i;<br /> file1&gt;&gt;i;//从文件输入一个整数值。 </p> <p>　　这种方式还有一种简单的格式化能力，比如可以指定输出为16进制等等，具体的格式有以下一些</p> <p>操纵符 功能 输入/输出 <br /> dec 格式化为十进制数值数据 输入和输出 <br /> endl 输出一个换行符并刷新此流 输出 <br /> ends 输出一个空字符 输出 <br /> hex 格式化为十六进制数值数据 输入和输出 <br /> oct 格式化为八进制数值数据 输入和输出 <br /> setpxecision(int p) 设置浮点数的精度位数 输出 </p> <p>　　比如要把123当作十六进制输出：file1&lt; </p> <p>　　2、<strong style="color: black; background-color: #99ff99">二进制</strong>文件的读写<br /> &#9312;put()<br /> put()函数向流写入一个字符，其原型是ofstream &amp;put(char ch)，使用也比较简单，如file1.put('c');就是向流写一个字符'c'。 </p> <p>&#9313;get()<br /> get()函数比较灵活，有3种常用的重载形式：</p> <p>　　一种就是和put()对应的形式：ifstream &amp;get(char &amp;ch);功能是从流中<strong style="color: black; background-color: #a0ffff">读取</strong>一个字符，结果保存在引用ch中，如果到文件尾，返回空字符。如file2.get(x);表示从文件中<strong style="color: black; background-color: #a0ffff">读取</strong>一个字符，并把<strong style="color: black; background-color: #a0ffff">读取</strong>的字符保存在x中。</p> <p>　　另一种重载形式的原型是： int get();这种形式是从流中返回一个字符，如果到达文件尾，返回EOF，如x=file2.get();和上例功能是一样的。</p> <p>　　还有一种形式的原型是：ifstream &amp;get(char *buf,int num,char delim='  ')；这种形式把字符读入由 buf 指向的数组，直到读入了 num 个字符或遇到了由 delim 指定的字符，如果没使用 delim  这个参数，将使用缺省值换行符' '。例如：</p> <p>　　file2.get(str1,127,'A');//从文件中<strong style="color: black; background-color: #a0ffff">读取</strong>字符到字符串str1，当遇到字符'A'或<strong style="color: black; background-color: #a0ffff">读取</strong>了127个字符时终止。</p> <p>&#9314;读写数据块<br /> 要读写<strong style="color: black; background-color: #99ff99">二进制</strong>数据块，使用成员函数read()和write()成员函数，它们原型如下：</p> <p>　　　　read(unsigned char *buf,int num);<br /> write(const unsigned char *buf,int num);</p> <p>　　read()从文件中<strong style="color: black; background-color: #a0ffff">读取</strong> num 个字符到 buf 指向的缓存中，如果在还未读入 num 个字符时就到了文件尾，可以用成员函数 int gcount();来取得实际<strong style="color: black; background-color: #a0ffff">读取</strong>的字符数；而 write() 从buf 指向的缓存写 num 个字符到文件中，值得注意的是缓存的类型是 unsigned char *，有时可能需要类型转换。</p> <p>例：</p> <p>　　　　unsigned char str1[]="I Love You";<br /> int n[5];<br /> ifstream in("xxx.xxx");<br /> ofstream out("yyy.yyy");<br /> out.write(str1,strlen(str1));//把字符串str1全部写到yyy.yyy中<br /> in.read((unsigned char*)n,sizeof(n));//从xxx.xxx中<strong style="color: black; background-color: #a0ffff">读取</strong>指定个整数，注意类型转换<br /> in.close();out.close(); </p> <p>四、检测EOF<br /> 成员函数eof()用来检测是否到达文件尾，如果到达文件尾返回非0值，否则返回0。原型是int eof();</p> <p>例：　　if(in.eof())ShowMessage("已经到达文件尾！");</p> <p>五、文件定位<br /> 和C的文件操作方式不同的是，C++  I/O系统管理两个与一个文件相联系的指针。一个是读指针，它说明输入操作在文件中的位置；另一个是写指针，它下次写操作的位置。每次执行输入或输出时，  相应的指针自动变化。所以，C++的文件定位分为读位置和写位置的定位，对应的成员函数是 seekg()和  seekp()，seekg()是设置读位置，seekp是设置写位置。它们最通用的形式如下：</p> <p>　　　　istream &amp;seekg(streamoff offset,seek_dir origin);<br /> ostream &amp;seekp(streamoff offset,seek_dir origin); </p> <p>　　streamoff定义于 iostream.h 中，定义有偏移量 offset 所能取得的最大值，seek_dir 表示移动的基准位置，是一个有以下值的枚举： </p> <p>ios::beg：　　文件开头 <br /> ios::cur：　　文件当前位置 <br /> ios::end：　　文件结尾 <br /> 这两个函数一般用于<strong style="color: black; background-color: #99ff99">二进制</strong>文件，因为文本文件会因为系统对字符的解释而可能与预想的值不同。</p> <p>例：</p> <p>file1.seekg(1234,ios::cur);//把文件的读指针从当前位置向后移1234个字节<br /> file2.seekp(1234,ios::beg);//把文件的写指针从文件开头向后移1234个字节 </p> </div> </div> 	</div></div><img src ="http://www.cppblog.com/KaneCpp/aggbug/193664.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/KaneCpp/" target="_blank">Kane</a> 2012-10-22 12:02 <a href="http://www.cppblog.com/KaneCpp/articles/193664.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>