C语言程序设计教程第12章.doc_第1页
C语言程序设计教程第12章.doc_第2页
C语言程序设计教程第12章.doc_第3页
C语言程序设计教程第12章.doc_第4页
C语言程序设计教程第12章.doc_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

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

文档简介

231文 件12.1 文 件 概 述12.1.1 文件的概念及文件分类图12.1 文件的结构表12.1 整数2248存储方式字符2248ASCII码00110010001100100011010000111000表12.2 内存中存储形式整数 2248二进制形式000010001100100012.1.2 文件系统图12.2 缓冲文件系统12.1.3 文件指针typedef struct short level; /*记录缓冲区满或空的程度*/ unsigned flags; /*文件状态标志*/ char fd; /*与文件相关的标识符,即文件句柄*/ unsigned char hold; /*当缓冲区中无数据时(level=0)不读取字符*/ short bsize; /*缓冲区大小,默认为512字节*/ unsigned char *buffer; /*文件缓冲区的指针*/ unsigned char *curp;/*当前激活缓冲区的指针*/ unsigned istemp; /*临时文件标识*/ short token; /*常用于文件有效性检查*/ FILE;12.2 文件的打开与关闭12.2.1 文件的打开表12.3 文件的打开模式打开模式说明 r只读,打开一个文本文件w只写,建立一个文本文件a追加,向文本文件末尾添加数据rb只读,打开一个二进制文件wb只写,建立一个二进制文件ab追加,向二进制文件末尾添加数据r+读写,打开一个文本文件,可读/写w+读写,打开一个文本文件,先写后读a+读写,打开一个文本文件,可读、追加写rb+读写,打开一个二进制文件,可读/写wb+读写,打开一个二进制文件,先写后读ab+读写,打开一个二进制文件,可读、追加写12.2.2 文件的关闭12.3 文件的读写12.3.1 字符读写函数#include void main( ) char ch; FILE *fp; if(fp=fopen(myfile1.txt, w+)=NULL) printf(cannot open filen); exit(0); printf(Please input the string: ); ch=getchar(); while(ch!=#) fputc(ch,fp); ch=getchar(); fclose(fp);图12.3 用“记事本”打开的myfile1.txt文件#include void main( ) char ch; FILE *fin,*fout; if(fin=fopen(myfile1.txt, r)=NULL) /*以只读方式打开源文件*/ printf(cannot open filen); exit(0); if(fout=fopen(myfile2.txt, w)=NULL) /*以只写方式打开目标文件*/ printf(cannot open filen); exit(0); ch=fgetc(fin);/*从源文件中读取一个字符*/ while(ch!=EOF)/*当文件未结束时*/ fputc(ch,fout);/*向目标文件中写入一个字符*/ ch=fgetc(fin); fclose(fin);/*关闭源文件*/ fclose(fout);/*关闭目标文件*/12.3.2 字符串读写函数#include void main( ) char str320= Beijing, Shenyang,Haerbin,temps20; int i; FILE *fp; if(fp=fopen(myfile3.txt, w)=NULL) printf(cannot open filen); exit(0); for(i=0;i3;i+) fputs(stri,fp); /*写入字符串*/ fputc(n,fp); /*写入一分隔符*/ fclose(fp); if(fp=fopen(myfile3.txt, r)=NULL) printf(cannot open filen); exit(0); printf(The file content is:n); while(fgets(temps,20,fp)!=NULL)/*读入字符串*/ printf(%s,temps); fclose(fp);12.3.3 格式化读写函数#include void main( ) int stuid,score,stunum,i; float average; FILE *fp; printf(输入学生数:); scanf(%d,&stunum); if(fp=fopen(stuscore.txt, w)=NULL) printf(cannot open filen); exit(0); printf(输入%d个学生的信息(学号 成绩):n,stunum); for(i=1;i=stunum;i+) scanf(%d%d,&stuid,&score); fprintf(fp,%d %dn,stuid,score); fclose(fp); if(fp=fopen(stuscore.txt, r)=NULL) printf(cannot open filen); exit(0); stunum=0;average=0.0; printf(%6s%6sn,学号,成绩); while(fscanf(fp,%d%d,&stuid,&score)!=EOF) /*读入成功时,即未到文件尾*/ stunum+; average+=score; printf(%6d%6dn,stuid,score); fclose(fp); average/=stunum; printf(平均成绩:%6.2fn,average);12.3.4 数据块读写函数struct StuType int id; char name15; int age; int score;StuInfo30;图12.4 例12.5程序的模块功能结构图#include struct StuType /*学生信息类型*/ int id; char name15; int age; int score;StuInfo100; /*全局量,用于存储学生信息*/int StuNum; /*全局量,用于存储学生个数*/void Menu( );void InputInfo( );void DisplayInfo();void WriteInfo( );void ReadInfo( );void main( ) /*主函数*/ int select; while(1) Menu( ); scanf(%d,&select); switch(select) case 1: InputInfo(); WriteInfo(); break; /*输入信息并存盘*/ case 2: ReadInfo(); DisplayInfo(); break; /*读取信息并显示*/ case 0: return; /*退出*/ void Menu( ) /*主界面*/ printf(*n); printf( Student Information System n); printf(*n); printf( Main Menu n); printf( 1-Input information n); printf( 2-Display information n); printf( 0-Exit n); printf( Select:);void InputInfo( ) /*学生信息输入*/ int i; printf(nInput the number of Students:); scanf(%d,&StuNum); printf(Please input info(Id Name Age Score):n); for(i=0;iStuNum;i+) scanf(%d%s%d%d,&StuInfoi.id,StuI,&StuInfoi.age, &StuInfoi.score); void DisplayInfo( ) /*学生信息显示*/ int i; printf(n-*Student Information*-nn); printf(%4s %14s %4s %5sn,Id,Name,Age,Score); for(i=0;iStuNum;i+) printf(%4d %14s %4d %5dn,StuInfoi.id,StuI, StuInfoi.age,StuInfoi.score); printf(n);void WriteInfo( ) /*将学生信息输出至文件*/ FILE *fp; int i; if(fp=fopen(student.dat, wb)=NULL) /*以只写方式打开二进制文件*/ printf(cannot open filen); return; if(fwrite(&StuNum,sizeof(int),1,fp)!=1) /*向文件中写入学生数*/ printf(Write error!n); fclose(fp); return; for(i=0;iStuNum;i+) /*利用循环向文件中写入学生信息*/ if(fwrite(&StuInfoi,sizeof(struct StuType),1,fp)!=1) printf(Write error!n); break; fclose(fp);void ReadInfo( ) /*从文件中读取学生信息*/ FILE *fp; int i; if(fp=fopen(student.dat, rb)=NULL) /*以只读方式打开二进制文件*/ printf(Cannot open filen); return; if(fread(&StuNum,sizeof(int),1,fp)!=1) /*读入学生数*/ printf(Read error!n); fclose(fp); return; for(i=0;iStuNum;i+) /*利用循环向从文件中读入学生信息*/ if(fread(&StuInfoi,sizeof(struct StuType),1,fp)!=1) printf(Read error!n); break; fclose(fp);图12.5 运行例12.5程序并输入学生信息图12.6 运行例12.5程序并显示学生信息12.4 文件读写指针的定位及文件检测12.4.1 文件读写位置指针的概念12.4.2 文件读写位置指针的定位表12.4 使用fseek时起始点的表示及含义符号常量数字代表的起始点SEEK_SET0文件开始SEEK_END2文件末尾SEEK_CUR1文件当前位置#include struct StuType int id; char name15; int age; int score;void PrintStu(struct StuType stu) /*输出一个学生信息*/ printf(%d %s %d %dn,stu.id,,stu.age,stu.score);void main( ) FILE *fp; int InfoSize; struct StuType stu; if(fp=fopen(stu.dat, rb)=NULL) printf(cannot open filen); exit(0); InfoSize=sizeof(struct StuType); fseek(fp,(long)(2*InfoSize),SEEK_SET); /*将文件位置指针移至第3个学生的数据处*/ fread(&stu,InfoSize,1,fp); /*读取一个同学信息后,文件位置指针移至第4个学生的数据处*/ PrintStu(stu); fseek(fp,(long)(InfoSize),SEEK_CUR); /*将指针文件位置指针移至第5个同学的数据处*/ fread(&stu,InfoSize,1,fp); PrintStu(stu); fseek(fp,(long)(-2*InfoSize),SEEK_END); /*将文件位置指针移至倒数第2个同学的数据处*/ fread(&stu,InfoSize,1,fp); PrintStu(stu); fclose(fp);#include struct StuType int id; char name15; int age; int score;void PrintStu(struct StuType Stu) /*输出一个学生信息*/ printf(%4d %14s %4d %5dn,Stu.id,S,Stu.age,Stu.score);void main( ) struct StuType Stu; FILE *fp; int StuNum,i,InfoSize; if(fp=fopen(student.dat, ab+)=NULL) printf(cannot open filen); exit(0); InfoSize=sizeof(struct StuType); printf(n*Append information*n); printf(Input the number of students:); scanf(%d,&StuNum); printf(Please input info(Id Name Age Score):n); for(i=0;iStuNum;i+) /*输入学生信息,并追加至文件中*/ scanf(%d%s%d%d,&Stu.id,S,&Stu.age,&Stu.score); fwrite(&Stu,InfoSize,1,fp); printf(n*Display information*n); printf(%4s %14s %4s %5sn,Id,Name,Age,Score); fseek(fp,0L,SEEK_END); /*将文件读写指针移至文件尾*/ StuNum=ftell(fp)/InfoSize; /*求文件中存储的学生信息个数*/ rewind(fp); /*将文件读写指针置于文件头*/ for(i=0;iStuNum;i+)/*读入学生信息并显示*/ fread(&Stu,InfoSize,1,fp); PrintStu(Stu); fclose(fp);12.4.3 文件的检测#include struct StuType int id; char name15; int age; int score;void PrintStu(struct StuType Stu) printf(%4d %14s %4d %5dn,Stu.id,S,Stu.age,Stu.score);void main( ) struct StuType Stu; int InfoSize; FILE *fp; if(fp=fopen(student.dat, rb)=NULL) printf(cannot open filen); exit(0); InfoSize=sizeof(struct StuType); printf(n*Display information*n); printf(%4s %14s %4s %5sn,Id,Name,Age,Score); fread(&Stu,InfoSize,1,fp); /*读取一个学生信息*/ while(!feof(fp) /*当文件结束时*/ PrintStu(Stu); fread(&Stu,InfoSize,1,fp); fclose(fp);#include void main( ) char ch; FILE *fp; if(fp=fopen(myfile4.txt, r)=NULL) printf(cannot open filen); exit(0); ch=fgetc(fp); while(feof(fp) /*当文件未结束时*/ putchar(ch); ch=fgetc(fp); fclose(fp); #include void main( ) FILE *fp; char ch; fp=fopen(file1.c,w); /*以只写方式打开文件*/ ch=fgetc(fp); /*从文件读取一个字符,读操作出错*/ if(ferror(fp) /*当具有读写错时*/ printf(File error.n); clearerr(fp); /*清除读写错误*/ if(!ferror(fp) printf(Error cleared.n); fclose(fp); 习 题 12#includ

温馨提示

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

评论

0/150

提交评论