C程序 语言文件_第1页
C程序 语言文件_第2页
C程序 语言文件_第3页
C程序 语言文件_第4页
C程序 语言文件_第5页
已阅读5页,还剩81页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Intelligent Information Processing Lab., Dept.of Computer Sci. /* 定义文件指针 */ fp=fopen() ; /* 打开文件,fp指向该文件控制信息 */ /* 文件读写 */ flocse(fp) ; /* 关闭文件,结束对指定文件的存取操作 */,Intelligent Information Processing Lab., Dept.of Computer Sci. filename: 字符串,需要使用的文件名 mode: 字符串,文件使用方式 (see:【p333】Tab.13-1) 阅读【pp333-334】(1)

2、-(8),File Opening ,Intelligent Information Processing Lab., Dept.of Computer Sci. discard previous contents if any 产生新文件 ”a”: append; open or create text file for writing at end of file 如果文件不存在则产生新文件,追加数据,File Opening discard previous contents if any 产生新文件 ”a+”: append; open or create text file for

3、update, writing at end 如果文件不存在则产生新文件,写时追加数据,File Opening ,Intelligent Information Processing Lab., Dept.of Computer Sci. , 可否按二进制方式打开?,Intelligent Information Processing Lab., Dept.of Computer Sci. char fname81=d:mytext.txt; FILE *fp; if ( (fp=fopen(fname, w)=NULL ) puts(n文件打开失败!); return ; for (ch=

4、A; ch=Z; ch+) if ( fputc(ch, fp)!=ch ) puts(n文件写入错误!); return; fclose(fp); puts(n文件写入完毕!); return; ,打开文件,写入一个字符,关闭文件,Intelligent Information Processing Lab., Dept.of Computer Sci. ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,超出文件末尾字节而继续读时返回非0值(读无效),否则返回0(读有效),Intelligent Informat

5、ion Processing Lab., Dept.of Computer Sci. ,文件1,文件2,in,out,copy,已超出文件最后一个字节,但此时feof(in)=0,因此!feof(in)=1,将多复制1个垃圾字节,应该采用【pp335】程序段的思路!,Intelligent Information Processing Lab., Dept.of Computer Sci. char ch; char infile20=d:mytext.txt; char outfile20=d:newtext.txt; unsigned long int byte=0; /* 复制的字节数

6、*/ if ( (in=fopen(infile, rb)=NULL ) puts(“n不能打开源文件!); return; if ( (out=fopen(outfile, wb)=NULL ) puts(n不能打开目标文件!); return; ,while ( ch=fgetc(in), feof(in)=0 ) fputc(ch, out); byte+; fclose(in); fclose(out); printf(n%lu个字节复制完毕, byte); return; ,教材【pp337】例13.2中定义了ch却没使用!?!?,逗号表达式, 值为feof(in)=0的值,两个文件

7、分别都要关闭,Intelligent Information Processing Lab., Dept.of Computer Sci. ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,程序数据区,磁盘,缓冲区满或文件关闭时,文件,fp,字节数:size,buffer,块数: count,FILE类型 信息区域,文件打开,001101011100,Intelligent Informa

8、tion Processing Lab., Dept.of Computer Sci. 功能: 输入n名学生数据并存放到st指向的(共享存储区)n个结构体数据区域中 save函数对fwrite()的包装 原型: unsigned int save(SCORE *st, int n, char *fname); 功能: 把st所指的数据区内的数据全部写入文件(fname指向文件名字符串),一个结构体对象为数据块,n为数据块个数;返回文件字节数,File Reading int sub3 ; ; typedef struct score SCORE ; void input(SCORE *, in

9、t); unsigned int save(SCORE *, int, char *); void main() SCORE studentN ; /* 开辟内存空间 */ char file20=”d:score.fil” ; unsigned int byte ; input(student, N) ; /* 输入数据*/ byte=save(student, N, file); /* 保存到文件 */ if ( byte!=N*sizeof(SCORE) printf(”n数据保存失败”); else printf(”n数据保存成功, 文件字节数:%u”, byte); ,Intelli

10、gent Information Processing Lab., Dept.of Computer Sci. for (p=st; pno, p-sub, p-sub+1, p-sub+2) ; return ; ,Intelligent Information Processing Lab., Dept.of Computer Sci. FILE *fout ; if ( (fout=fopen(fname, ”wb”)=NULL ) printf(”n文件%s创建失败!”, fname) ; return 0; count=fwrite(st, sizeof(SCORE), n, fou

11、t) ; fclose(fout); return count*sizeof(SCORE) ; ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,程序数据区,磁盘,001101011100,缓冲区空或没有所需数据时,文件,fp,字节数:size,buffer,块数

12、: count,FILE类型 信息区域,文件打开,Intelligent Information Processing Lab., Dept.of Computer Sci. 功能: 从文件(fname指向文件名字符串)读入数据,以一个结构体对象为数据块,以n为数据块个数,存放到st所指的数据区内(构成有n个元素的结构体数组);返回读入的字节数 output函数 原型: void input(SCORE *st, int n); 功能: 对数组中的n名学生成绩进行平均值计算,并显示,File Reading int sub3 ; ; typedef struct score SCORE ; v

13、oid output(SCORE *, int); unsigned int load(SCORE *, int, char *); void main() SCORE studentN ; /* 开辟内存空间 */ char file20=”d:score.fil” ; unsigned int byte ; byte=load(student, N, file); /* 读文件 */ if ( byte!=N*sizeof(SCORE) printf(”n数据调入失败”); else printf(”n数据调入成功, 文件字节数:%u”, byte); output(student, N)

14、 ; /* 统计并输出数据*/ ,为读入数据准备“场地”,Intelligent Information Processing Lab., Dept.of Computer Sci. for (p=st; psub+i); printf(”n学号:%s 平均成成绩:%f”, p-no, sco/3.0); return ; ,Intelligent Information Processing Lab., Dept.of Computer Sci. FILE *fin ; if ( (fin=fopen(fname, ”rb”)=NULL ) printf(”n文件%s打开失败!”, fnam

15、e) ; return 0; count=fread(st, sizeof(SCORE), n, fin) ; return count*sizeof(SCORE) ; ,Intelligent Information Processing Lab., Dept.of Computer Sci. ,while (ch=fgetc(fp1), !feof(fp1) putchar(ch);,getc是stdio.h中定义的宏,可认为功能与fgetc相同,Intelligent Information Processing Lab., Dept.of Computer Sci. 功能: 把文件指针

16、fp指定的文件当前数据读写位置以base为基准相距offset字节的位置;定位失败时返回值为非0,成功时为0 offset的取值: 长整型,常量后加L,正(负)数表示向文件末尾(开头)方向移动 base的取值: SEEK_SET、SEEK_CUR、SEEK_END(这些符号常量在stdio.h中定义为0、1、2) 阅读【pp346】例13.5,File Positioning,60,rewind(fp) fseek(fp, 0L, SEEK_SET),0101010101010111101010110110101101111101,SEEK_SET,SEEK_END,SEEK_CUR,offs

17、et = -2,offset = +2,Intelligent Information Processing Lab., Dept.of Computer Sci. 功能: 返回文件指针fp指定的文件当前数据读写位置,检测失败时返回-1L,File Positioning,61,Intelligent Information Processing Lab., Dept.of Computer Sci. 功能: 返回指定文件(由fname指定文件名)的字节数(磁盘占有量) 算法 以只读方式打开文件 数据读写位置定位到文件末尾 返回当前ftell()的值,File Positioning,62,0

18、101010101010111101010110110101101111101,SEEK_END,ftell(fp)=5,位置指针,Intelligent Information Processing Lab., Dept.of Computer Sci. long int byte ; if ( (fp=fopen(fname, ”rb”)=NULL ) return -1L; if ( fseek(fp, 0L, SEEK_END)!=0 ) return -1L; byte=ftell(fp); fclose(fp) ; return byte ; ,可否写成return ftell(f

19、p); ?,Intelligent Information Processing Lab., Dept.of Computer Sci. void main() long int size ; if ( (size=fsize(”d:mytext.txt”)=-1L ) puts(”n文件长度检测失败”); else printf(”n文件长度为%ld字节”, size); ,Intelligent Information Processing Lab., Dept.of Computer Sci. 功能:如果最近一次对fp指定文件的I/O操作发生了错误,则返回非0值,否则返回0,Error Handling,66,Intelligent Information Processi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论