C语言程序设计(第2版)课件 蔺德军16 文件_第1页
C语言程序设计(第2版)课件 蔺德军16 文件_第2页
C语言程序设计(第2版)课件 蔺德军16 文件_第3页
C语言程序设计(第2版)课件 蔺德军16 文件_第4页
C语言程序设计(第2版)课件 蔺德军16 文件_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

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

文档简介

文件

本章主要内容文件类别及打开方式文件打开文件关闭fputcfgetc函数freadfwrite函数fprintffscanf函数文件指针定位文件类别及打开方式文件类别:ASCII文件(文本),二进制文件文件处理过程1.打开2.读写3.关闭文本文件与二进制文件区别文件打开打开函数:FILE*fopen(文件名,读写方式)打开成功返回文件指针,否则返回空指针文件名和读写方式都是字符串,如:FILE*fp;fp=fopen(“C:\\f1.txt”,”r”)读写方式:组合:r,w,a,r+,w+,a+,rb,wb,ab,rb+,wb+,ab+文件关闭读写文件的内容,是先放在缓冲区内,在关闭文件时,内容才真正被写入磁盘文件,因此一个文件没有正常关闭,可能会丢失数据文件关闭函数intfclose(FILE*fp);正常关闭返回0,关闭失败返回非0值文件结束判断:intfeof(FILE*fp);//到达文件末尾返回1,否则返回0也可以通过读出的字符判断,文件结束字符ASCII值为-1或0xff文件存在判断函数,在IO.H中:intaccess(char*filename,intmode);成功返回0否则返回-1mode0存在;1执行;2写,4读,6读写;读写字符fputc,fgetc格式:intfputc(intch,FILE*fp);

intfgetc(FILE*fp);成功返回读写值,不成功返回-1;例#include<stdio.h>#include<process.h>intmain(){chari,c;FILE*fpr,*fpw;fpw=fopen("c:\\tt\\text1.txt","w");if(fpw==NULL){printf("openerror");exit(0);}for(i='a';i<='z';i++)fputc(i,fpw);fclose(fpw);fpr=fopen("c:\\tt\\text1.txt","r");fpr=fopen("c:\\tt\\text2.txt","w");if((fpw==NULL)||(fpr==NULL)){printf("openerror");exit(0);}while(!feof(fpr)){c=fgetc(fpr);fputc(c+3,fpw);}fclose(fpr);fclose(fpw);}在一个文件内写入a~z26个字符,然后读出,把每个字符ASCII加3后写入另一个文件fwrite、freadunsignedfwrite(void*buffer,unsignedsize,unsignedcount,FILE*fp)unsignedfread(void*buffer,unsignedsize,unsignedcount,FILE*fp)四个参数分别是:存放数据首地址,每个单元字节数,读写多少个单元,读写文件指针fwrite、fread举例#include<stdio.h>typedefstructstudent{intno,age;};studentst;intmain(){inti;FILE*fp;fp=fopen("c:\\rr.dat","wb");for(i=0;i<3;i++){cout<<"another\n";scanf("%d%d",&st.no,&st.age);fwrite(&st,sizeof(st),1,fp);}fclose(fp);fp=fopen("c:\\rr.dat","rb");while(!feof(fp)){fread(&st,sizeof(st),1,fp);if(!feof(fp))printf("no:%d,age,%d\n",st.no,st.age);}fclose(fp);}fprintf、fscanffprintf(FILE*fp,char*format,args);fscanf(FILE*fp,char*format,args);函数使用和printfscanf相似,只能读写ASCII文件fprintffscanf举例//查看写的文件,找出错原因#include<stdio.h>intmain(){ FILE*fp; inta,b; fp=fopen("D:\\t\\tf.txt","w"); scanf("%d%d",&a,&b);//输入23 fprintf(fp,"%d%d",a,b);//文件内23成一个数 fclose(fp); fp=fopen("D:\\t\\tf.txt","r"); fscanf(fp,"%d%d",&a,&b); fclose(fp); printf("a=%d,b=%d\n",a,b); }fgets、fputschar*

fgets(char*s,intlen,FILE*fp);intfputs(char*s,FILE*fp);

文件指针定位正常读写过程中,文件指针自动下移,移到下一次应读写的位置也可以使用文件指针移动函数,人为移动文件指针voidrewind(FILE*fp)使文件指针指向文件开头intfseek(FILE*fp,longoffset,intbase);从base移动offset个字节,正值向后移,负值向前移,移动成功返回1,否则返回非0base值可选项:SEEK_SET(文件头)SEEK_CUR(当前指针位置)SEEK_END(文件尾)辅助函数:longftell(FILE*fp);获得文件指针当前位置文件指针定位文件随机读写示例#include<stdio.h>#include<process.h>#include<string.h>structstudent{intid;charname[10];}st1;intmain(){intno=0,n;charname[10];FILE*fp;fp=fopen("c:\\tmp1.txt","wb+");if(NULL==fp){printf("openerror");exit(0);}while(no!=-1){scanf("%d%s",&no,name);st1.id=no;strcpy(,name);if(st1.id!=-1)fwrite(&st1,sizeof(st1),1,fp);};rewind(fp);while(!feof(fp)){fread(&st1,sizeof(st1),1,fp);if(!feof(fp))printf("%d%s\n",st1.id,);}rewind(fp);n=0;while(n!=-1){printf("seekNo?");

scanf("%d",&n);if(n!=-1){if(fseek(fp,(n-1)*sizeof(st1),SEEK_SET)==0){printf("%d\n",ftell(fp));fread(&st1,sizeof(st1),1,fp);printf("%d%s\n",st1.id,);}}elseprintf("notfind\n");}fclose(fp);}修改记录举例#include<stdio.h>#include<process.h>#include<string.h>structstudent{intid;charname[10];}st1;voidCreateFile(char*FileName){ intno=0;charname[10];FILE*fp;fp=fopen(FileName,"wb");if(NULL==fp){printf("openerror");exit(0);}while(no!=-1) {scanf("%d%s",&no,name);st1.id=no;strcpy(,name);if(st1.id!=-1)fwrite(&st1,sizeof(st1),1,fp); }; fclose(fp);}voidShowFile(char*FileName){FILE*fp;fp=fopen(FileName,"rb");while(!feof(fp)){fread(&st1,sizeof(st1),1,fp);if(!feof(fp))printf("%d%s\n",st1.id,);}fclose(fp);}voidChangeByRecNo(inti,intid,char*name,char*FileName)//根据记录号改{FILE*fp;fp=fopen(FileName,"rb+");fseek(fp,(i-1)*sizeof(st1),SEEK_SET);st1.id=id;strcpy(,name);//printf("now%d%s\n",st1.id,);fwrite(&st1,sizeof(st1),1,fp);fclose(fp);}voidChangeByID(intid,char*name,char*FileName)//根据no改{FILE*fp;fp=fopen(FileName,"rb+");while(!feof(fp)){fread(&st1,sizeof(st1),1,fp);//printf("r%d%s\n",st1.id,);if(st1.id==id) { //printf("find%d%s\n",st1.id,); strcpy(,name); fseek(fp,-sizeof(st1),SEEK_CUR);

温馨提示

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

评论

0/150

提交评论