第14章文件流和内存流.ppt
《第14章文件流和内存流.ppt》由会员分享,可在线阅读,更多相关《第14章文件流和内存流.ppt(39页珍藏版)》请在三一文库上搜索。
1、第14章 文件流和内存流,14.1 文件及文件流的基本概念 文件:程序+数据+文档,外部设备的总称(aaa.c null prn con) 14.1.1 输入和输出 内存向外存: 输出、写、存 外存向内存: 输入、读、取 14.1.2 文件的存储方式 ASCII码: 1024=49 48 50 52 =0100 1001 0100 1000 0101 0000 0101 0010 二进制存储方式: 1024 =0000 0100 0000 0000 文件流: 专门用于磁盘文件I/O的流类(专门用于磁盘文件I/O的类)。,14.1.3 文件流类体系 类filebuf用于管理文件的缓冲区。 类fs
2、treambase提供了I/O所需要的所有成员函数。 类ifstream用于从文件中取(i)数据。 类ofstream用于向文件输出(o)数据。 类fstream用于文件的双向数据传递I/O)。 以上类的派生(产生)方法参看305页。,14.2 文件的打开与关闭 14.2.1 文件的打开 C+在文件流类中提供了4个文件的成员函数: void fstreambase:open(char *, int, int=filebuf :openprot) void ifstream :open(char *, int=ios : in, int) void ofstream : open(char *,
3、 int=ios : out, int) void fstream : open(char *, int, int),说明: 第一个参数是文件名,第二个参数是访问方式,第三个参数是保护方式。 在所有文件流的基类ios中,可以使用下列打开模式的公有枚举成员: in 0x01 输入 out 0x02 输出 ate 0x04 将指针置于文件未 app 0x08 追加 binary 0x80 打开二进制文件 一般情况,将几种模式组合使用。更多的成员参考306页。,例:打开一个文件result文件 ifstream infile; infile.open(“result”); 等价于: infile.o
4、pen(“result”,ios :in); /打开输入文件 infile.open(“result”,ios :in | ios : : binary); /打开二进制输入文件,说明: 对于类ifstream的对象,打开文件时默认模式in。 对于类ofstream的对象,打开文件时默认模式out。 C+在文件类中提供的4个文件流类中同时重载一个带缺省参数的构造函数: void fstreambase :fstreambase (char *, int, int=filebuf :openprot) void ifstream :ifstream (char *, int=ios :out,
5、int) void ofstream :ofstream (char *, int=ios :in, int) void fstream :fstream (char *, int, int) 定义对象时,同时也打开了一个文件。,4. 若文件打开出错,则返回一个NULL值。因此常常用下列格式提示出错: ifstream infile; infile.open(“result”); if (! infile) cour(“cannot open this file”); exit(1); 其中,exit(1) 为返回编辑状态,1可省。,14.2.2 文件的关闭 ifstream infile;
6、infile.open(“result”); if (! infile) cour(“cannot open this file”); exit(1); infile. close();,14.3 文件的读写 使用两个读写运算符: 例14-1:模拟DOS命令,将*.cpp 再复制一份,新文件名为:* new.cpp 算法: 打开输入文件 打开输入/输出文件 文件复制 文件关闭,方法一: #include “fstream.h“ #include “iostream.h“ #include “stdlib.h“ void main(int argc,char *argv) if (argc!=3
7、) cout“input error“endl; exit(1); ,ifstream infile(argv1); /定义对象并初始化,打开模式省 if (! infile) coutch) outfilech; infile.close(); outfile.close(); ,方法二: #include “fstream.h“ #include “iostream.h“ #include “stdlib.h“ void main(int argc,char *argv) if (argc!=3) cerr“input error“; exit(1); fstream infile,out
8、file; /先定义文件类对象 infile.open(argv1,ios:in); /不是构造函数,不能省略模式 outfile.open(argv2,ios:out);,if (!infile|!outfile) cerr“Cont open file “endl; exit(1); char ch; while (infile.get(ch) outfile.put(ch); infile.close(); outfile.close(); ,14.5 文件读写函数 14.5.1 二进制文件读写函数 istream 说明:第一个参数为内容,第二个参数为读写的字节数。,例14-2:用二进制
9、读写函数完成文本文件的复制 #include #include “stdlib.h“ void main(int argc,char *argv) if (argc!=3) cerr“input error“; exit(1); fstream infile, outfile; infile.open(argv1,ios:in | ios:binary); outfile.open(argv2,ios:out | ios:binary);,if (! infile | ! outfile) /文件打开有错,对象为0 cerr“Cont open file “endl; exit(1); cha
10、r ch1024; do infile.read(ch,1024); if(infile.eof() /文件结束为真 int n=infile.gcount(); /把最后不够1024字节时的字节取出 outfile.write(ch,n); break; ,else outfile.write(ch,1024); while(1); infile.close(); outfile.close(); ,例14-3:用二进制读写函数完成数据文件的写(p318) 生成10个数,进行排序,然后保存到文件中 #include #include “stdlib.h” #include #include
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 14 文件 内存
链接地址:https://www.31doc.com/p-2251135.html