第12章文件【练习12-1】读出例12-1学生成绩文件f12-1.txt内容,输出最高分和最低分及相应的学号和姓名。解答:#includestdio.h#includestdlib.hstructstudent{longnum;charstname[20];intscore;};intmain(void){FILE*fp;inti,max,min,j=0,k=0;structstudentstudents[5];if((fp=fopen(f12-1.txt,r))==NULL){printf(Fileopenerror!\n);exit(0);}fscanf(fp,%ld%s%d,&students[0].num,students[0].stname,&students[0].score);max=min=students[0].score;for(i=1;i=4;i++){fscanf(fp,%ld%s%d,&students[i].num,students[i].stname,&students[i].score);if(maxstudents[i].score){max=students[i].score;j=i;}if(minstudents[i].score){min=students[i].score;k=i;}}printf(Maxscore:%d,num:%d,name:%s\n,students[j].score,students[j].num,&students[j].stname);printf(Minscore:%d,num:%d,name:%s\n,students[k].score,students[k].num,&students[k].stname);if(fclose(fp)){printf(Cannotclosethefile!\n);exit(0);}return0;}【练习12-2】请使用例8-9答电码加密函数对民吗字符串进行加密,改写例12-2。解答:#includestdio.h#includestring.h#includestdlib.hstructsysuser{charusername[20];charpassword[8];};voidencrypt(char*pwd);intmain(void){FILE*fp;inti;structsysusersu;if((fp=fopen(f12-2.txt,w))==NULL){printf(Fileopenerror!\n);exit(0);}for(i=1;i=5;i++){printf(Enter%dthsysuser(namepassword):,i);scanf(%s%s,su.username,su.password);encrypt(su.password);fprintf(fp,%s%s\n,su.username,su.password);}if(fclose(fp)){printf(Cannotclosethefile!\n);exit(0);}return0;}voidencrypt(char*pwd){inti;for(i=0;istrlen(pwd);i++)if(pwd[i]=='z')pwd[i]='a';elsepwd[i]+=1;}【练习12-3】例12-3中为什么在执行fputc(ch,fp2)前要判断ch的值是否等于EOF?改写例12-3的程序,在复制用户信息文件后,再统计被复制文件中字符的数量。解答:文件结束符EOF是一个值为-1的常量,读文件时可用来判断从文件中读入的字符是否为EOF来决定循环是否继续。#includestdio.h#includestdlib.hintmain(void){FILE*fp1,*fp2;charch;intcount=0;if((fp1=fopen(f12-2.txt,r))=NULL){printf(Fileopenerror!\n);exit(0);}if((fp2=fopen(f12-3.txt,w))==NULL){printf(Fileopenerror!\n);exit(0);}while(!feof(fp1)){ch=fgetc(fp1);if(ch!=EOF){fputc(ch,fp2);count++;}}if(fclose(fp1)){printf(Cannotclosethefile!\n);exit(0);}if(fclose(fp2)){printf(Cannotclosethefile!\n);exit(0);}printf(f12-2中字符数量为:%d,count);return0;}【练习12-4】字母转换并统计行数:读取一个指定的文本文件,显示在屏幕上,如果有大写字母,则改成小写字母再输出,并根据回车符统计行数。试编写相应程序。解答:#includestdio.h#includestdlib.hintmain(void){charch;intcountline=0;FILE*fp;if((fp=fopen(练习12-4.txt,r))==NULL){printf(Notopen!);exit(0);}while(!feof(fp)){ch=fgetc(fp);if(ch!=EOF)if(ch='A'&&ch='Z')printf(%c,ch-'A'+'a');elseprintf(%c,ch);if(ch=='\n')countline++;}printf(\n);printf(file'slineis:%d.,countline+1);if(fclose(fp)){printf(Cannotclose!);exit(0);}return0;}【练习12-5】写字符并验证:从键盘输入一行字符,写入到文件f3.txt中,并重新读出,最终在屏幕上显示验证。程序输入以读到回车符“\n”为结束,读文件时要用EOF来控制循环。试编写相应程序。解答:#includestdio.h#includestdlib.hintmain(void){FILE*fp;charch;if((fp=fopen(f3.txt,w+))==NULL){printf(cannotopenfile!);exit(0);}printf(Inputthestring:\n);ch=getchar();while(ch!='\n'){fputc(ch,fp);ch=getchar();}rewind(fp);while(!feof(fp)){ch=fgetc(fp);if(ch!=EOF)putchar(ch);}printf(\n);if(fclose(fp)){printf(cannotclosefile!\n);exit(0);}return0;}【练习12-6】实数取整写入文件:文件f1.txt中有若干个实数,请分别读出,将每个实数按四舍五入取整后存入文件f2.txt中。试编写相应程序。解答:#includestdio.h#includestdlib.hintmain(void){FILE*fp1,*fp2;doublea;if((fp1=fopen(f1.txt,r))==NULL){printf(Fileopenerror!\n);exit(0);}if((fp2=fopen(f2.txt,w))==NULL){printf(Fileopenerror!\n);exit(0);}while(!feof(fp1)){fscanf(fp1,%lf,&a);fprintf(fp2,%.0f,a);}if(fclose(fp1)){printf(Cannotclosethefile!\n);exit(0);}if(fclose(fp2)){printf(Cannotclosethefile!\n);exit(0);}return0;}【练习12-7】修改例12-6,增加修改资金账户的功能。输入一个记录ID,如果文件中已存在该记录,则输入新的记录信息并更新资金账户文件中相应记录的信息。要求定义和调用函数UpdateLog(),其功能是修改资金账户记录。解答:#includestdio.h#includestdlib.hlongsize;structLogData{longlogid;charlogdate[11];char1ognote[15];doublecharge;doublebalance;};intinputchoice(){intmychoice;printf(\nEnteryourchoice:\n);printf(1-AddanewcashLOG.\n2-ListAllCashLOG.\n);printf(3-QueryLastCashLoG.\n0-Endprogram.\n);scanf(%d,&mychoice);returnmychoice;}longgetLogcount(FILE*cfptr){longbegin,end,logcount;fseek(cfptr,OL,SEEK_SET);begin=ftell(cfptr);fseek(cfptr,size,SEEK_END);end=ftell(cfptr);logcount=(end-begin)/size-1;returnlogcount;}/*列出所有收支流水账*/voidListAllLog(FILE*cfptr){structLogDatalog;fseek(cfptr,OL,SEEK_SET);fread(&log,size,1,cfptr);printf(logidlogdatelognotechargebalance\n);while(!feof(cfptr)){printf(%6ld%-11s%-15%10.2lf%10.2lf\n,log.logid,log.logdate,log.lognote,log.charge,log.balance);fread(&log,size,1,cfptr);};}/*查询显示最后一条记录*/voidQueryLastLog(FILE*cfptr){structLogDatalog;longlogcount;logcount=getlogcount(cfptr);if(1ogcount0){fseek(cfptr,size*(logcount-1),SEEK_SET);fread(&log,size,1,cfptr);printf(Thelastlogis:\n);printf(logid:%-6ld\nlogdate:%-11s\nlognote:%-15s\n,log.logid,log.logdate,log.lognote);printf(charge:%-10.2lf\nbalance:-10.2lf\n,log.charge,1og.balance);}elseprintf(nologsinfile!\n);}/*添加新记录*/voidAddNewLog(FILE*cfptr){structLogDatalog,lastlog;longlogcount;printf(Inputlogdate(format:2006-01-01):);scanf(%s,log.logdate);printf(Inputlognote:);scanf(%s,log.lognote);printf(InputCharge:Income+andepend-:);scanf(%lf,&log.charge);logcount=getLogcount(cfptr);if(logcount0){fseek(cfptr,size*(logcount-1),SEEK_SET);fread(&lastlog,size,1,cfptr)log.logid=las