版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第十三章 文 件File management,掌握文件以及缓冲文件系统、文件指针的概念; 学会使用文件打开、关闭、读、写等文件操作函数。,Why we have to use file?,1. It becomes cumbersome(麻烦) and time consuming to handle large volumes of data through terminals. 2. The entire data is lost when either the program is terminated or the computer is turned off.,13.1 C文件概述
2、File in C,一、文件的概念 Concept of the file,文件:是指存储在外部介质上数据的集合。 A file is a place on the disk where a group of related data is stored.,操作系统是以文件为单位对数据进行管理的。每一个与主机相连的输入或输出设备都看作是一个文件。,二、文件的存在形式及分类,文件的存在形式:文件名+文件内容。,文件内容:是一个字符(字节)序列,即由一个个字符 (字节)的数据顺序组成。对文件的存取是以字 符(字节)为单位的,这类文件称为流式文件。,文件的分类:File category,ASCII
3、 码文件(文本文件):文件的每一个字节放 一个ASCII代码,代表一个字符。 Text File 二进制文件 :把内存中的数据按其在内存中的存储形式原样输出到文件上。 Binary File,根据数据在内存的组织形式不同可分为两类文件,三、文件的基本操作,(1) 打开文件把文件名等目录信息从磁盘上读 入内存并存入结构体。 (2) 关闭文件把内存结构体中的文件名等目录 信息写入磁盘。 (3) 写文件 向文件写内容。 (4) 读文件 从文件读内容。,四、文件的输入输出方式,C语言对文件进行处理分为: 非缓冲文件系统 no buffer file system 缓冲文件系统 buffer file
4、system,非缓冲文件系统程序与外设直接进行数据交换。,特点每读写一次都要启动外设。,缓冲文件系统程序与外设通过内存缓冲区进行 数据交换。(常用的方式),特点只有内存缓冲区满(写盘)或空(读盘)才 启动外设。,13.2 C文件类型指针,一、文件类型(FILE type),每个被使用的文件都在内存中开辟一个区,用来存放文件的有关信息(如文件名,状态,当前位置等)。这些信息保存在一个结构体类型的变量中。结构体类型由系统定义为“FILE”。 Data structure of a file is defined as FILE in the library of standard I/O func
5、tion definitions. Therefore, all files should be declared as FILE before they are used. FILE is a defined data type.,二、定义FILE类型指针变量 FILE definition,FILE 类型的指针变量定义方法,FILE *指针变量名; FILE *pointer_variable_name;,例如:FILE *fp; 含义:fp是一个指向FILE类型结构体的指针变量。 fp as a pointer to the data type FILE.,可以使fp指向某个文件的结构体
6、变量,从而通过该结构体变量中的文件信息能够访问该文件。,13.3 文件的打开和关闭函数,一、文件的打开函数(fopen函数),(1) 调用方式:,FILE *fp ; fp= fopen(文件名,文件的使用方式);,所要打开的文件名。必须是用双引号引起的字符串常量。,具有特定含意的符号也必须由双引号引起,用户定义的文件类型指针变量,fp= fopen(“filname”,“mode”);,功能:以指定的方式打开指定的文件,若操作 成功,则返回一个指向该文件的指针,若打 开文件时出现错误,则返回空指针NULL。,例 FILE *fp; fp=fopen(“mydata.txt,r);,Opera
7、tion: Creates a new file for use. Opens an existing file for use. If operating success, the function will return a pointer to the file, else it will return an empty pointer of NULL.,文件使用方式,基本方式字符(单独使用是对文本文件):,r 只读文件(读),如无文件,产生出错信息。 If the purpose is reading, and if it exists, then the file is opened
8、 with the current contents safe otherwise an error occurs. w 建立新文件(写),如有同名文件,删除之。 When the mode is writing,a file with the specified name is created if the file does not exist. The contents are deleted, if the file already exists. a 写旧文件(添加),如无旧文件,则生成该文件。 When the mode is appending, the file is open
9、ed in the current contents safe. A file with the specified name is created if the file does not exist.,按二进制格式读写文件(基本方式字符+“b”),b 辅助说明,按二进制格式读写文件。,同时进行读写文件(基本方式字符+“b”+“+”),+ 辅助说明,可同时进行读和写文件。 r+ The existing file is opened to the beginning for both reading and writing. w+ Same as w except both for read
10、ing and writing. a+ Same as a except both for reading and writing.,例如:fp=fopen(“mydata.txt”,“rb”);,例如:fp=fopen(“mydata.txt”,“r+”);,例:#include main() FILE *fp; if(fp=fopen(“mydata.txt,r) =NULL) printf(file not found.); exit(0); else printf(file opened ok!); fclose(fp); ,常使用此方法打开一个文件,exit函数的功能是关闭所有文件,
11、终止程序运行。exit(0)为正常终止程序,Instruction: fp=fopen(“mydata.txt,r) if(fp =NULL),程序运行结果: 如果磁盘上存在文件mydata.txt,屏幕上会显示 file open ok!,否则屏幕上显示file not found.。,二、文件的关闭函数(fclose函数) CLOSE A FILE,(1) 调用方式:,fclose(文件指针); fclose(file_pointer), 功能:关闭由文件指针指定的文件,防止它被误用;同时把缓冲区中的数据(未装满缓冲区的数据)输出到磁盘上,释放文件指针。 A file must be cl
12、osed as soon as all operations on it have been completed. This ensures that all outstanding information associated with the file is flushed out(冲洗) from the buffers and all links to the file are broken.,例如: FILE *fp; fp=fopen(“output.dat”,“r+”); /* 进行读写操作 */ fclose(fp);,注意:fopen函数和fclose函数总是成对出现的。 无
13、fclose函数时会导致部分数据丢失!,13.4 文件的读写函数 Input/Output Operations on Files,一、fputc函数 和fgetc函数,这两个函数是以字符为单位进行文件读写的函数。 These are analogous to getchar and putchar functions and handle one character at a time.,功能:把指定字符存入文件指针所指的文件中。 如果输出失败,则返回一个EOF(-1)。 (EOF是在stdio.h文件中定义的符号常量,值为-1),例如:fputc(ch,fp); (其中ch为字符变量,fp
14、为文件指针) 含义:将ch的值输出到fp所指向的文件中去。 Write the character contained in the character variable ch to the file associated with FILE pointer fp. If the output failed, the fputc will return a marker of EOF.,1. fputc函数,一般调用形式:,fputc(字符常量或变量,文件指针); fputc(character constant or variable,file_pointer),功能:从文件指针所指文件中读
15、一个字符赋给指 定的字符变量。当遇文件结束符,返回一 个文件结束标志EOF(-1)。 The file pointer moves by one character position for every operation of fgetc or fputc. The fgetc will return an end-of-file marker EOF, when end of the file has been reached.,例如:ch=fgetc(fp); 其中ch为字符变量,fp为文件指针。 含义:从fp所指向的文件中读入1个字符赋给字符变量ch。,2. fgetc函数,一般调用形
16、式:,字符变量=fgetc(文件指针); ch=fgetc(fp);,例: 阅读程序。 #include stdio.h #define NULL 0 main() FILE *fp; int count=0; char c; if(fp=fopen(“file.txt”,“r”)=NULL) printf(“Cant Open File n”); exit(0); do c=fgetc(fp); count+; while(c!=EOF); count-; printf(“count=%dn”,count); fclose(fp); ,若文件file.txt的内容是: abcd1234ABC
17、D,输出结果:,12,这是读一个文本文件中字符,并判断文件是否结束的常用方法,关于文件读写状态的检测 Error Handling During I/O Operations,函数调用是否成功,可有两种手段来检测: 1. 由函数的返回值来确定 用文件状态检测函数feof、ferror、fclearerr feof(fp) :测试fp所指向的文件的位置是否是文件尾。 若已到达文件尾,函数返回非零值,若没有到达文件尾,则返回0。 feof(fp) will return a non-zero value after the pointer fp pointes to the end of file
18、. Otherwise it will return 0.,常用程序段: while(!feof(fp) ,if(feof(fp) printf(“End of data.n”);,if(ferror(fp)!=0) printf(“An error has occured.n”);,ferror(fp) :测试对fp所指向的文件的操作是否出 错。若出错,函数返回非零值,否则 返回0。,fclearerr(fp) :使ferror和feof函数值置0。,二、 fread函数和fwrite函数,一般调用形式:,fread(buffer,size,count,fp);,fwrite(buffer,
19、size,count,fp);,其中:,buffer:是一个指针(起始地址)。 对fread来说,它是读入数据的存放地址。 对fwrite来说,它是输出数据的地址。,size :要读写的字节数。,count:要进行读写多少个size字节的数据项。,功能:对fp所指的文件读写buffer所指的size*count 个字节数据。,例如:fread(f,4,2,fp); (其中f是实型数组名),例1 把数组a写入文件fa.txt;再从fa读入数组b。 #include stdio.h main() FILE* fp; int a100=1,5,6,78,21,34,67,87,23,b100, i
20、; fp=fopen(fa.txt,wb); fwrite(a,sizeof(a),1,fp); /* fwrite(a,sizeof(int),100,fp);*/ fclose(fp); fp=fopen(fa.txt,rb); fread(b,sizeof(a),1,fp); for(puts(),i=0;i10;i+) printf(%6d,bi); fclose(fp); ,三、 fprintf 函数和fscanf函数,这两个函数是进行格式读写文件的函数 。,一般调用形式:,fprintf(文件指针,格式字符串,输出表列);,fscanf(文件指针,格式字符串,输入地址) ;,fp
21、is a file pointer associated with a file that has been opened for writing. The control string contains output specifications for the items in the list. The list may include variables, constants and strings.,fprintf(fp,”control string”,list);,例如:fprintf(fp, %d,%6.2f,i,x);,功能:按指定格式将数据写到指定文件中。 或从指定文件按格
22、式输入数据。 The functions fprintf and fscan perform I/O operations, except of course that they work on files.,又如:fscanf(fp, %d,%f,#include stdio.h void main() char infile15, outfile15,radar10,func6; FILE *fp1,*fp2; printf(请输入雷达数据文件名(*.txt)n); scanf(“%s”,infile); if(fp1=fopen(infile,r)=NULL) printf(cannot
23、 open this filen); exit(0); ,AN_TPS-43E.txt 4000脉冲功率(kw) 6310最大增益 0.3125带宽(MHz) 3中心频率(GHz) 0.1天线高度(km) 440最大作用距离(km) 5对应散射截面(m2) 0.5对应发现概率 1e-006对应虚警概率 1.1水平波束宽度(deg) 4.8垂直波束宽度(deg) 20仰角扫描范围(deg) 6天线转速(rpm) 1斯威林模型(14) 6.5脉冲宽度(us) 250脉冲重复频率(Hz) 2脉冲积累数 1.20472e+023系统特征常数(m2),fscanf(fp1,%s,radar); fsca
24、nf(fp1,%s,func); fscanf(fp1,%f, ,printf(请输入输出数据文件名(*.txt)n); scanf(%s,outfile); if(fp2=fopen(outfile,w)=NULL) printf(cannot open this filen); exit(0); ,fprintf(fp2,Radar:%s, Function=%s, Frequency=%5.1f MHz ,radar,func,frequ); fprintf(fp2, PRF=%5.1f Hz, Tao=%4.1f us, RPM=%4.1f r/minn ,prf,tao,rpm);
25、fprintf(fp2,swerling_case:%d, rmax=%6.1f km, sigma0=%5.1fdB, PD0=%5.1f, P_fa=%e, N number=%dn ,swerling_case,rmax,sigma0,pd0,p_fa,N); for(j=0;jX;j+) fprintf(fp2,%4.0f,dmsm ,sigmaj); fprintf(fp2,n); for(j=0;jZ;j+) for(i=0;iX;i+) fprintf(fp2,t%6.4f,pddij); fprintf(fp2,n); fclose(fp2); ,四、 fputs函数 和fgets函数,这两个函数是以字符串为单位进行
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年山西省吕梁市孝义市第二次中考模拟考试卷英语
- 科技研发工程师项目技术突破与创新绩效评估表
- 建筑地面防水处理手册
- 《陈涉世家》文言知识及理解性默写内容归纳
- 考城管笔模拟试题目及答案
- 考评员培训测模拟试题及参考答案
- 脑肺吸虫病模拟试题及参考答案
- 计算机基础第一章测模拟试题及答案
- 技术规格变更通知及设计调整要求说明函(7篇)范文
- 迈瑞D3除颤仪培训模拟试题及答案
- 物业管理费收缴制度及账目管理规范
- 湘西吊脚楼教学课件
- 《走向世界的中国标志》+课件-2025-2026学年浙人美版(2024)初中美术八年级上册
- 营口新山鹰大空间灭火监控系统培训讲解
- 新警安全保密教育培训课件
- 中药保留灌肠课件
- 2025年数智供应链案例集-商务部
- 学校保安闭环管理制度
- 北京市科技计划项目(课题)结题经费审计工作底稿-参考文本
- 电子商务物流说课省公开课一等奖全国示范课微课金奖课件
- CJ/T 123-2016 给水用钢骨架聚乙烯塑料复合管
评论
0/150
提交评论