c++文件打开和关闭

news/2024/7/20 22:17:11 标签: c++, ios, buffer, file, mfc, c
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="htmledit_views"> cii-font-family: 宋体">一般使用fstream 类中所定义的成员函数 open( )和 close( )。
class="O" style="mso-margin-left-alt: 216"> cii-font-family: 宋体"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">fstream outclass="tags" href="/tags/FILE.html" title=file>file; 定义一个fstream类的对象
class="O" style="mso-margin-left-alt: 216"> cii-font-family: 宋体">cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">cerun: yes">  outclass="tags" href="/tags/FILE.html" title=file>file.open("f1.txt", class="tags" href="/tags/IOS.html" title=ios>ios::out);cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN"> 
class="O" style="mso-margin-left-alt: 216"> cii-font-family: 宋体">cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">或者 cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">fstream outclass="tags" href="/tags/FILE.html" title=file>file("f1.txt", class="tags" href="/tags/IOS.html" title=ios>ios::out);cerun: yes">                      
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">incerun: yes">                 cii-font-family: 'Times New Roman'">以输入(读)方式打开文件
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">outcerun: yes">               cii-font-family: 'Times New Roman'">以输出(写)方式打开文件
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">appcerun: yes">               cii-font-family: 'Times New Roman'">以输出追加方式打开文件
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">atecerun: yes">                cii-font-family: 'Times New Roman'">文件打开时࿰c;文件指针位于文件尾
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">trunecerun: yes">             cii-font-family: 'Times New Roman'">如果文件存在࿰c;将其长度截断为 cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">0 cii-font-family: 'Times New Roman'">࿰c;并清除原有
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'">cerun: yes">                     cii-font-family: 'Times New Roman'">内容;如果文件不存在࿰c;则创建新文件
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">Binarycerun: yes">           cii-font-family: 'Times New Roman'">以二进制方式打开文件࿰c;缺省时为文本方式
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">nocreatecerun: yes">       cii-font-family: 'Times New Roman'">打开一个已有文件࿰c;如该文件不存在࿰c;打开失败
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">noreplacecerun: yes">     cii-font-family: 'Times New Roman'">如果文件存在࿰c;除非设置 cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">class="tags" href="/tags/IOS.html" title=ios>ios::ate cii-font-family: 'Times New Roman'">或 cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">class="tags" href="/tags/IOS.html" title=ios>ios::app, cii-font-family: 'Times New Roman'">否则打
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'">cerun: yes">                     cii-font-family: 'Times New Roman'">开操作失败
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">class="tags" href="/tags/IOS.html" title=ios>ios::in|class="tags" href="/tags/IOS.html" title=ios>ios::outcerun: yes">             cii-font-family: 'Times New Roman'">以读和写的方式打开文件
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">class="tags" href="/tags/IOS.html" title=ios>ios::out|class="tags" href="/tags/IOS.html" title=ios>ios::binarycerun: yes">      cii-font-family: 'Times New Roman'">以二进制写方式打开文件
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">class="tags" href="/tags/IOS.html" title=ios>ios::in|class="tags" href="/tags/IOS.html" title=ios>ios::binarycerun: yes">        cii-font-family: 'Times New Roman'">以二进制读方式打开文件
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'">
class="O" style="mso-margin-left-alt: 336"> cii-font-family: 'Times New Roman'">未指定 cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">binary cii-font-family: 'Times New Roman'">方式时࿰c;文件都以文本方式打开,若指定了 cii-font-family: 'Times New Roman'">cerun: yes">   binary cii-font-family: 'Times New Roman'">方式࿰c;则文件以二进制方式打开.
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'">cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN"> 
class="O" style="mso-margin-left-alt: 216; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">cii-font-family: 'Times New Roman'">打开文件的另一种方法 利用fstream的专门读或写的子类
class="O" style="mso-margin-left-alt: 216; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1">
class="O" style="mso-margin-left-alt: 336; mso-line-spacing: '90 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'">cerun: yes">            ofstream outclass="tags" href="/tags/FILE.html" title=file>file("f1.txt", class="tags" href="/tags/IOS.html" title=ios>ios::out);cerun: yes">                     cii-font-family: 'Times New Roman'">写
class="O" style="mso-margin-left-alt: 336; mso-line-spacing: '90 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'">cerun: yes">            ifstream istrm("f2.txt");cerun: yes">                                      cii-font-family: 'Times New Roman'">读
class="O" style="mso-margin-left-alt: 336; mso-line-spacing: '90 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'">
class="O" style="mso-margin-left-alt: 384; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'">读写完不要忘了关闭文件
cii-font-family: 'Times New Roman'">
class="O" style="mso-margin-left-alt: 216; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">inclass="tags" href="/tags/FILE.html" title=file>file.close( );
class="O" style="mso-margin-left-alt: 216; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">//
class="O" style="mso-margin-left-alt: 216; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">例如:
class="O" style="mso-margin-left-alt: 216; mso-line-spacing: '80 20 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"> cii-font-family: 'Times New Roman'; mso-fareast-language: ZH-CN">MFC主要读写文件的类是CFile,而CStdioFile类是派生自CFile类的࿰c;主要增加了一个按行的方式读取/写入文件每行字符串的功能!别的地方没咋研究!

//CFile读取所有文件到缓冲区:
CFile class="tags" href="/tags/FILE.html" title=file>file;
char class="tags" href="/tags/BUFFER.html" title=buffer>buffer[1024];
if(!class="tags" href="/tags/FILE.html" title=file>file.Open("c://aaa.txt",CFile::modeRead))return; //打开aaa这个文件࿰c;打开形式为读取
class="tags" href="/tags/FILE.html" title=file>file.Read(class="tags" href="/tags/BUFFER.html" title=buffer>buffer,1024); //将文件的内容从开始࿰c;读1024到缓冲区(如果想读取不同地方的数据则需要设置游标函数Seek,SeekToBegin,SeekToEnd..)
class="tags" href="/tags/FILE.html" title=file>file.Close();

//CFile写内容到文件
CFile class="tags" href="/tags/FILE.html" title=file>file;
char class="tags" href="/tags/BUFFER.html" title=buffer>buffer[1024] = "在此添加要写入文件的字符串!";
if(!class="tags" href="/tags/FILE.html" title=file>file.Open("c://aaa.txt",CFile::Create | CFile::Write))return; //打开aaa这个文件࿰c;打开形式为(无此文件则新建)写入
class="tags" href="/tags/FILE.html" title=file>file.Write(class="tags" href="/tags/BUFFER.html" title=buffer>buffer,strlen(class="tags" href="/tags/BUFFER.html" title=buffer>buffer));
class="tags" href="/tags/FILE.html" title=file>file.Close();

//CStdioFile类的例子:
CStdioFile class="tags" href="/tags/FILE.html" title=file>file;
CString str; //StdioFile可以直接使用字符串类CString的类型
if(!class="tags" href="/tags/FILE.html" title=file>file.Open("c://aaa.txt",CFile::modeRead))return; //打开aaa这个文件࿰c;打开形式为读取
class="tags" href="/tags/FILE.html" title=file>file.ReadString(str); //读一行内容到str字符串࿰c;注意此时文件的游标将自动设置到下一行的开头位置࿰c;所以如果想读下一行还是用class="tags" href="/tags/FILE.html" title=file>file.ReadString(str);
class="tags" href="/tags/FILE.html" title=file>file.Close();
class="O" style="mso-margin-left-alt: 336; mso-line-spacing: '100 50 0'">
cle>

http://www.niftyadmin.cn/n/700366.html

相关文章

java 继承 三小一大_请教:国家二级(JAVA)笔试模拟试卷24第1大题第3小题如何解答?...

本题的功能是用按钮来控制文本框中文本的颜色。窗口中有两个带有文字标题的面板“Sample text”和“Text color control”,窗口的底部还有一个复选按钮“Disable changes”。在“Sample text”面板中有一个带有字符串的文本框,而在“Text color control”…

[ITIL学习笔记]之事件管理(1)

[Incident Management][事件管理]-------------------------------------------------------------[Goal/Mission of Incident Management]To restore normal service operation as quickly as possible and minimize the adverse impact on business operations, thus ensurin…

Team Building

上个星期五部门里一起去阳澄湖吃大闸蟹了,师出有名,Team Building.胃病略有好转的我也是兴致勃勃地在7点半就赶到公司楼下。可惜天公并不作美,迷雾的天气,使得能见度大概只有不到200米。于是在高速公路上停留了大约2个小时。再赶到…

BOOL和bool的区别

BOOL是微软定义的typedef int BOOL。与bool不同,它是一个三值逻辑,TRUE/FALSE/ERROR,返回值为>0的整数为TRUE,0为FALSE,-1为ERROR。Win32 API中很多返回值为BOOL的函数都是三值逻辑。比如GetMessage(). 三&#xff…

Action for FLEX

词法分析器采取的操作当词法分析器与说明文件规则部分中的一个扩展正则表达式匹配时,它执行与扩展正则表达式相对应的操作。没有足够的规则匹配输入流中的所有字符串,词法分析器则将输入复制到标准输出。因此,不要创建仅将输入复制到输出的规…

职业人提高工作效率的几种途径

(1)学会管理和规划时间。任何时候,职业人应当能够自由支配时间,而不是被时间所支配。具体来说,就是每天都要制定工作计划,预估时间消耗,按优先级进行排序,并依次来实施,否则必定造成手忙脚乱的情…

SendMessage函数完全使用手册

首先 我们了解一下Windows的消息机制。Windows是一个消息驱动式系统,Windows消息提供应用程序与应用程序之间,应用程序与Windows系统之间进行通信的手段。 举个例子,打开记事本程序,该程序有一个 “文件“菜单。 那么&#xf…

Web服务器和应用程序服务器有什么区别

Web服务器和应用程序服务器有什么区别[ 来源:javaworld.com | 作者:佚名 | 时间:2006-10-12 14:08:30 | 浏览:人次 ]  问:什么是应用程序服务器,什么是Web服务器,它们有什么不同?  答:  通俗的讲&am…