C语言程序设计何钦铭_第1页
C语言程序设计何钦铭_第2页
C语言程序设计何钦铭_第3页
C语言程序设计何钦铭_第4页
C语言程序设计何钦铭_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

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

文档简介

1、第12章文件【练习12-1读出例12-1学生成绩文件内容,输出最髙分和最低分及相应的学 号和姓名。 解答: ttincludeO#include struct student long num;%char stname20;int score;;int main(void)FILE *fp;int iTmax,mintj=0fk=0; struct student students ;if(fp二fopen(,r)二二NULL) printf (,rFile open error!n,r): exit (0);fscanf (fp,&students0. num,students0. stna

2、me,&students01 score);max二min二students0| score;for(i=l;i=4;i+)?fseanf (fp,d%s%d, &students i num, students i stname, &students i score);i f(maxstudentsi score) min=studentsi score; k=i;printf (,fMaxscore: %dt num:%dt name:%sn,r, students! j score, students jJ num,&studen tsj stname);printf (,rM insc

3、ore: %d,num:%d,name:%sn,studentsk score,studentsk num,&studen tsk stname);if(fclose(fp) printf(,FCan not close the file!n); exit (0);return 0;【练习12-2】请使用例8-9答电码加密函数对民吗字符串进行加密,改写例12-2。 解答:ftincludeOftincludeOttincludeOsstruct sysuser char username20:char password8:;void encrypt (char *pwd):int main(v

4、oid)FILE *fp;int i;struct sysuser su;if(fp二fopen,w)=NULL)printf(File open error!n);exit (0);for(i=l;i=5;i+) printf (,rEnter %dth sysuser (name password): i);scanf (%s%s,;encrypt;fprintf (fpt ,r%s %sn;if(fclose(fp) printfCCan not close the file!n,r);exit (0);return 0;void encrypt(char *pwd)int i;for

5、(i=0; Kstrlen(pwd) ; i+) if(pwdi=z)pwdi=a:elsepwdi+=l;)【练习12-3例12-3中为什么在执行fputc (ch, fp2)前要判断ch的值是否 等于EOF改写例12-3的程序,在复制用户信息文件后,再统计被复制文件中字 符的数量。解答:文件结束符EOF是一个值为-1的常量,读文件时可用来判断从文件中读入的字 符是否为EOF来决定循环是否继续。#include#includeint main (void)FILE *fpl,*fp2;char ch;int count二0;if (fpl二fopen, ,r,)=NULL) printf(F

6、ile open error!n);exit (0);i f (fp2=fopen (w w,www) =NULL) printf(File open error!n,r):exit (0);while(!feof(fpl) ch=fgetc(fpl);if (ch!二EOF) fputc (ch,fp2); count+;if (fclose(fpl) printf(Can not close the file!n,r): exit (0);if (fclose(fp2) printf(Can not close the file!n); exit (0);printf (uf 12-2 中

7、字符数量为:%dN,count):return 0;【练习12-4】字母转换并统计行数:读取一个指定的文本文件,显示在屏幕上, 如果有大写字母,则改成小写字母再输出,并根据回车符统计行数。试编写相应 程序。解答:#include#includeint main(void)char ch;int countline=0;FILE *fp;if (fp=fopen(练习, r)=NULL) printf(Not open!);exit (0);while(!feof(fp)ch=fgetc(fp); if(ch!=EOF)if(ch=,A&ch=,Z,)printf(%c,ch-1A+ra); e

8、lseprintf(%c,ch);if(ch=,nr)countline+;printf(nnn);printf(files 1ine is:%d. ,countline+1);if(fclose(fp)printf(HCan not close!);exit (0);)return 0;【练习12-5写字符并验证:从键盘输入一行字符,写入到文件中,并重新读 出,最终在屏幕上显示验证。程序输入以读到回车符“n”为结束,读文件时要 用EOF来控制循环。试编写相应程序。解答:ftincludeO#includeint main (void)FILE *fp;char ch;if(fp二fopen(

9、,w+) =NULL)printf(can not open file!);exit (0);printf (,r Input the string: n,r):ch=getchar ();while(ch! =,n,) fputc(ch,fp);ch=getchar ();rewind(fp);while(!feof(fp)ch=fgetc(fp);if(ch!=E0F)putchar (ch);printf (nn,r);if(fclose(fp) printf(Mcan not close file!n);exit (0);return 0;【练习12-6实数取整写入文件:文件中有若干个

10、实数,请分别读出,将每个 实数按四舍五入取整后存入文件中。试编写相应程序。解答:#include#includeint main (void)FILE *fpl,*fp2;double a;if (fpl二fopen, ,rw)=NULL) printf(File open error!n);exit (0);)if (fp2=fopen(Wtt, ttwff)=NULL) printf(File open error!n);exit (0);while(!feof(fpl) fscanf(fpl,%lf,&a);fprintf (fp2,r%. Of a);)if(fclose(fpl) p

11、rintf (,rCan not close the file!n,r):exit (0);)if (fclose(fp2) printf(,rCan not close the file!n);exit (0);return 0;【练习12-7修改例12-6,增加修改资金账户的功能。输入一个记录ID,如果 文件中已存在该记录,则输入新的记录信息并更新资金账户文件中相应记录的信 息。要求定义和调用函数UpdateLog (),其功能是修改资金账户记录。解答:#include#includelong size;struct LogDatalong logid;!char logdatell:ch

12、ar lognote15;double charge;double balance;;int inputchoice()int mychoice;)printf(,rnEnter your choice: rT);printf (,r 1-Add a new cash LOG. n2List All Cash LOG. n,r);printf(3一Query Last Cash LoGnO-End programn);scanf (,r%d, &mycho i ce);return mychoice;long getLogcount(FILE *cfptr)long begin,end,log

13、count; fseek(cfptr,()L,SEEK SET): begin二ftell(cfptr); fseek (cfptr,size,SEEK END); end=ftell(cfptr); logcount二(end-begin)/sizeT; return logcount;/*列出所有收支流水账*/void ListAllLog(FILE *cfptr)struct LogData log;fseek(cfptr,OL,SEEK SET);fread(&1og,size,1,cfptr);printf (,r logid logdate log note charge bala

14、nce rT); while(!feof(cfptr) printf(,r%6 ld%-ll s%-l 5%n,rfread(&1og.size,1,cfptr);)/*查询显示最后一条记录*/void QueryLastLog(FILE *cfptr)struct LogData log;Ilong logcount;logcount=getlogcount(cfptr);if (logcount0)fseek(cfptr,size*(logcount-1),SEEK SET); fread (&1og.size,1,cfptr); printf(The last log is: n,r):

15、 pri ntf (logid:%-61dnlogdate:%Tlsnlognote:%T5sn,;?printf (,rcharge:%nbalance: n,r ;else printf (,rno logs in file!n,r):添加新记录*/void AddNewLog(FILE *cfptr)struct LogData log,last log;long logcount;printf (,r Input logdate (format: 2006-01-01):); scanf(”s,;printf (Tn put lognote: ,r): scanf (,r%s,r,:p

16、rintf(,rInput Charge: Income+and epend-:); scanf (%lf,&;logcount=getLogcount(cfptr);if(logcount0) fseek(cfptr,size*(logcount-l),SEEK SET);fread (&1astlog,size,1,cfptr)=+l;=+;else rewind(cfptr);ogid=last-taraetlastlog;printf (,r logid=%ldn,;fwirte(&1og,sizeof(struct LogData),1,cfptr);/*修改资金账户*/void U

17、pdateLog(FILE *cfptr)FILE *fpout;struct LogData user;char date11J:char note15:double charge;double balance;int choice,ID;cfptr=fi1eopen(r);if (fpout=fopen, Www)NULL) printf(can not open the file !n); exit (0);)printf (,rEnter LogID: n);scanf(%d,&ID);while(!feof(cfptr)fread(&user.sizeof(struct LogDat

18、a),1,cfptr); if (strcmp,ID)=0)printf (,r请输入修改信息:n);printf(wDate:w); scanf (,r%s,r. date); strcpy,date); printf(Note:);scanf (n%s,r. note);strcpy,note); printf(Charge:); =charge; printf (Balance:); scanf (,r%s,f,&balance); 二balance;fwrite(&user,sizeof(struct LogData),1,fpout);elsefwrite(&user,sizeof(

19、struct LogData),1,fpout); if (fclose(cfptr)printf(Hcan not close file!n): exit (0);if(fclose(fpout) printf(Hcan not close file!nH): exit (0);uni ink (,f,F) ; ; break;if(fclose(fp) printfCCan not close the file!n,r):exit (0);)return 0;习题12一、选择题1 .以下语句将输出B。#include%printf (%d %d %d,NULL, 0,E0F);0 10 -

20、1EOF0 EOF2如果二进制文件已经存在,现在要写入全新数据,应以B方式打开。A. wB. ,rwbnC. w+ D. “wb+3. 定义” FILE *fp;,则文件指针fp指向的是DoA.文件在磁盘上的读写位置B文件在级冲区上的读写位置C.整个磁盘文件D.文件类型结构4. 缓冲文件系统的文件缓冲区位于CoA.磁盘缓冲区中B.磁盘文件中C.内存数据区中D.程序文件中5. 使文件指针重新定位到文件读写的首地址的函数是Co() () () ()二、填空题1 函数fopen ()的返回值是指向文件缓冲区的首地址的文件结构类型指针 2文件的三大特征是名称大小_和_类型3 缓冲文件系统与非缓冲文件系

21、统的不同点在于系统是否为文件自动分配一 块文件缓冲区(内存单元)4. 只能向指定文件写入一个字符的函数是fputc()函数5. 判断文件指针是否已经到了文件尾部的函数是_feof()函数6阅读程序,以下程序完成的功能是文件infile内容复制到文件 include int main (void) char infile 10,outfile 10;FILE * fpa,* fpb;gets (infile);gets (outfile);fpa=fopen (infile, r1 );fpb=fopen (outfile,n w);while (! feof (fpa)fputc(fgetc(

22、fpa).fpb);fclose (fpa);fclose (fpb);return 0;三. 程序设计题1 统计文本文件中各类字符个数:分别统计一个文本文件中字母、数字及其他字 符的个数。试编写相应程序。解答: ftincludeO#includeint main (void)FILE *fp;char ch;int letter=0,digiter=0,other=0;if (fp=fopen(,r ,r, r)二二NULL) printf(File open error!n); exit (0);while (ch=fgetc(fp)!=E0F) if(ch=,A,&ch=,a&ch=,

23、0&ch=9,)digiter+;elseother+;printf(letter二%d,digiter=%d,other二%d,letter,digiter,other); if(fclose(fp) printf(,rCan not close the file!n);exit (0);return 0;2将实数写入文件:从键盘输人若干实数(以特殊数值-1结束),分别写到一个 文本文件中。试编写相应程序。解答:#includeS#includeint main(void)FILE *fp;int number;if(fp=fopen(,w)=NULL)printf(ucan not ope

24、n file!nn):exit (0); “ “printf(MInput number:n);scanf(u%d,&number);while (number!=1)fprintf(fp,%d,number);scanf(u%d,&number);)if(fclose(fp)printf(Can not close the file!n);exit (0);return 0; 3比较两个文本文件是否相等:比较两个文本文件的内容是否相同,并输出两个 文件中第一次出现不同字符内容的行号及列值。试编写相应程序。J解答: #include#include int main (void)FILE *f

25、plt*fp2; int i=l,j=l; char chitch2;if(fpl=fopen(u 0,urw)=NULL) printf(ucan not open file!nn): exit (0);i f (fp2=fopen (H rH) =NULL) printf(Hcan not open file!n); exit (0);chl=fgetc(fpl);ch2=fgetc(fp2);wh订e(chl!=E0F&ch2!=E0F)if(chl!=ch2)break;else if(chi!=n)j+;chl=fgetc(fpl);ch2=fgetc(fp2);elsechl=fg

26、etc(fpl);ch2=fgetc(fp2);printf C首次不同的字符在第d行、第d列。nij);if(fclose(fpl) printf(Hcan not close file!nM);exit (0);)if (fclose(fp2)printf(Mcan not close file!nM);exit (0);4. 将文件中的数据求和并写入文本文件尾:文件中存放了若干整数,将文件中 所有数据相加,并把累加和写入该文件的最后。试编写相应程序。解答:#include#includeint main (void)FILE *fp;int x,sum二0;if (fp=fopen(,

27、ab+)=NlLL) printf(ucan not open file!H):(exit (0);while(!feof(fp)fscanf(fp,%d,&x);sum+=x;)fprintf(fp,n %dH,sum);if(fclose(fp) Jprintf(Hcan not close the file!n);exit (0);return 0;5. 输出含for的行:将文本文件中所有包含字符串“for”的行输出。试编写相 应程序。解答:#include#includeftincludeOchar s999:int main(void)FILE* fp;int i;if(fp=fop

28、en(,r)=NULL)printf(ucan not open file!nH):exit (0);while(!feof (fp)fgets(s,999,fp);if(strlen(s)3) (for(i=0;iint main(void)FILE *fpl,*fp2;char ch,chl,ch2,s99;if (fpl=fopen( ,r,)=NULL) printf(ucan not open file!nn):exit (0);i f (fp2=fopen (H n, Hw,r) =NlLL) printf(Hcan not open file!nM): exit (0);whil

29、e(!feof(fpl)ch=fgetc(fpl);if(ch=/)if(chl=fgetc(fpl)=*)while(fgetc(fpl)!=*&(chi二fgetc(fpl)!=/) fseek(fpl,-sizeof (chi), 1);else if(chl=/)ch=*0;for(;chi!=n;chl=fgetc(fpl)chl0;else if(ch!=E0F) fputc (chF fp2);if (fclose(fp2) printf(ucan not close file!nH): exit (0);return 0; 7.(选做)账户余额管理:创建一个随机文件,用来存储银

30、行账户和余额信息, 程序要求能够查询某个账户的余额,当客户发生交易额时(正表示存入,负表示 取出)能够更新余额。账户信息包括账号、账号名和余额三个数据项。试编写相 应程序。文件部分内容如下:AcctNoAcctNameBalance1zhangsan1lisiwangwu解答:include #includeinclude long size;struct account char no10;char acctname50:double balance;;FILE *openfile(char *openmode)FILE *fp;if(fp=fopen(,openmode)=NULL)printf (nCan not open the file!,r): exit (0);return fp;double userbalanee(FILE *fp,char *name): void pay (FILE *fpt char *namer double count): int main()FILE *fp;int choice;char name50.:double balance;double count;printf(*请输入选择类型:n);printf (,r

温馨提示

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

最新文档

评论

0/150

提交评论