C语言程序设计第3版何钦铭 颜 晖 第12章文件.docx

上传人:b****6 文档编号:7109986 上传时间:2023-01-18 格式:DOCX 页数:24 大小:21.27KB
下载 相关 举报
C语言程序设计第3版何钦铭 颜 晖 第12章文件.docx_第1页
第1页 / 共24页
C语言程序设计第3版何钦铭 颜 晖 第12章文件.docx_第2页
第2页 / 共24页
C语言程序设计第3版何钦铭 颜 晖 第12章文件.docx_第3页
第3页 / 共24页
C语言程序设计第3版何钦铭 颜 晖 第12章文件.docx_第4页
第4页 / 共24页
C语言程序设计第3版何钦铭 颜 晖 第12章文件.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

C语言程序设计第3版何钦铭 颜 晖 第12章文件.docx

《C语言程序设计第3版何钦铭 颜 晖 第12章文件.docx》由会员分享,可在线阅读,更多相关《C语言程序设计第3版何钦铭 颜 晖 第12章文件.docx(24页珍藏版)》请在冰豆网上搜索。

C语言程序设计第3版何钦铭 颜 晖 第12章文件.docx

C语言程序设计第语言程序设计第3版何钦铭版何钦铭颜颜晖晖第第12章章文件文件第第12章章文件文件【练习12-1】读出例12-1学生成绩文件f12-1.txt内容,输出最高分和最低分及相应的学号和姓名。

解答:

#include#includestructstudentlongnum;charstname20;intscore;intmain(void)FILE*fp;inti,max,min,j=0,k=0;structstudentstudents5;if(fp=fopen(f12-1.txt,r)=NULL)printf(Fileopenerror!

n);exit(0);fscanf(fp,%ld%s%d,&students0.num,students0.stname,&students0.score);max=min=students0.score;for(i=1;i=4;i+)fscanf(fp,%ld%s%d,&studentsi.num,studentsi.stname,&studentsi.score);if(maxstudentsi.score)min=studentsi.score;k=i;printf(Maxscore:

%d,num:

%d,name:

%sn,studentsj.score,studentsj.num,&studentsj.stname);printf(Minscore:

%d,num:

%d,name:

%sn,studentsk.score,studentsk.num,&studentsk.stname);if(fclose(fp)printf(Cannotclosethefile!

n);exit(0);return0;【练习12-2】请使用例8-9答电码加密函数对民吗字符串进行加密,改写例12-2。

解答:

#include#include#includestructsysusercharusername20;charpassword8;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%sn,su.username,su.password);if(fclose(fp)printf(Cannotclosethefile!

n);exit(0);return0;voidencrypt(char*pwd)inti;for(i=0;istrlen(pwd);i+)if(pwdi=z)pwdi=a;elsepwdi+=1;【练习12-3】例12-3中为什么在执行fputc(ch,fp2)前要判断ch的值是否等于EOF?

改写例12-3的程序,在复制用户信息文件后,再统计被复制文件中字符的数量。

解答:

文件结束符EOF是一个值为-1的常量,读文件时可用来判断从文件中读入的字符是否为EOF来决定循环是否继续。

#include#includeintmain(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】字母转换并统计行数:

读取一个指定的文本文件,显示在屏幕上,如果有大写字母,则改成小写字母再输出,并根据回车符统计行数。

试编写相应程序。

解答:

#include#includeintmain(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(fileslineis:

%d.,countline+1);if(fclose(fp)printf(Cannotclose!

);exit(0);return0;【练习12-5】写字符并验证:

从键盘输入一行字符,写入到文件f3.txt中,并重新读出,最终在屏幕上显示验证。

程序输入以读到回车符“n”为结束,读文件时要用EOF来控制循环。

试编写相应程序。

解答:

#include#includeintmain(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中。

试编写相应程序。

解答:

#include#includeintmain(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(),其功能是修改资金账户记录。

解答:

#include#includelongsize;structLogDatalonglogid;charlogdate11;char1ognote15;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(logidlogdatelognotechargebalancen);while(!

feof(cfptr)printf(%6ld%-11s%-15%10.2lf%10.2lfn,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:

%-6ldnlogdate:

%-11snlognote:

%-15sn,log.logid,log.logdate,log.lognote);printf(charge:

%-10.2lfnbalance:

-10.2lfn,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=lastlog.1ogid+1;log.balance=log.charge+lastlog.balance;elselog.logid=1;log.balance=log.charge;rewind(cfptr);ogid=last-taraetlastlog;printf(logid=%ldn,log.logid);fwirte(&log,sizeof(structLogData),1,cfptr);/*修改资金账户*/voidUpdateLog(FILE*cfptr)FILE*fpout;structLogDatauser;chardate11;charnote15;doublecharge;doublebalance;intchoice,ID;cfptr=fileopen(r);if(fpout=fopen(cash.dat,w)=NULL)printf(cannotopenthefile!

n);exit(0);printf(EnterLogID:

);scanf(%d,&ID);while(!

feof(cfptr)fread(&user,sizeof(structLogData),1,cfptr);if(strcmp(user.logid,ID)=0)printf(请输入修改信息:

n);printf(Date:

);scanf(%s,date);strcpy(user.logdate,date);printf(Note:

);scanf(%s,note);strcpy(user.lognote,note);printf(Charge:

);user.charge=charge;printf(Balance:

);scanf(%s,&balance);user.balance=balance;fwrite(&user,sizeof(structLogData),1,fpout);elsefwrite(&user,sizeof(structLogData),1,fpout);if(fclose(cfptr)printf(cannotclosefile!

n);exit(0);if(fclose(fpout)printf(cannotclosefile!

n);exit(0);unlink(cashbox.dat);/remove(cashbox.dat);都是删除文件的函数rename(,cashbox.dat);/*打开文件*/FILE*openfile(char*openmode)FILE*fp;if(fp=fopen(cashbox.dat,openmode)=NULL)printf(cannotopenfilecashbox.dat!

n);exit(0);return(fp);intmain(void)FILE*fp;intchoice;size=sizeof(structLogData);while(choice=inputchoice()!

=0)switch(choice)case1:

fp=openfile(ab+);AddNewLog(fp);break;/*列出所有的收入支出情况*/case2:

fp=openfile(rb);ListAllLog(fp);break;/*查询最后记录及余额*/case3:

fp=openfile(rb);QueryLastLog(fp);break;case4:

fp=openfile(rb);UpdateLog(fp);break;default:

printf(InputError.);break;if(fclose(fp)printf(Cannotclosethefile!

n);exit(0);return0;习题121、选择题1.以下语句将输出_B_。

#includeprintf(%d%d%d,NULL,0,EOF);A.001B.00-1C.NULLEOFD.10EOF2.如果二进制文件a.dat已经存在,现在要写入全新数据,应以_B_方式打开。

A.wB.wbC.w+D.“wb+3.定义”FILE*fp;,则文件指针fp指向的是_D_。

A.文件在磁盘上的读写位置B文件在级冲区上的读写位置C.整个磁盘文件D.文件类型结构4.缓冲文件系统的文件缓冲区位于_C_。

A.磁盘缓冲区中B.磁盘文件中C.内存数据区中D.程序文件中5.使文件指针重新定位到文件读写的首地址的函数是_C_。

A.ftell()B.fseek()C.rewind()D.ferror()二、填空题1.函数fopen()的返回值是_指向文件缓冲区的首地址的文件结构类型指针_。

2.文件的三大特征是_名称_、_大小_和_类型_。

3.缓冲文件系统与非缓冲文件系统的不同点在于_系统是否为文件自动分配一块文件缓冲区(内存单元)_。

4.只能向指定文件写入一个字符的函数是_fputc()函数_。

5.判断文件指针是否已经到了文件尾部的函数是_feof()函数_。

6.阅读程序,以下程序完成的功能是_文件infile内容复制到文件_。

#includeintmain(void)charinfile10,outfile10;FILE*fpa,*fpb;gets(infile);gets(outfile);fpa=fopen(infile,r);fpb=fopen(outfile,w);while(!

feof(fpa)fputc(fgetc(fpa),fpb);fclose(fpa);fclose(fpb);return0;三、程序设计题1.统计文本文件中各类字符个数:

分别统计一个文本文件中字母、数字及其他字符的个数。

试编写相应程序。

解答:

#include#includeintmain(void)FILE*fp;charch;intletter=0,digiter=0,other=0;if(fp=fopen(12-,r)=NULL)printf(Fileopenerror!

n);exit(0);while(ch=fgetc(fp)!

=EOF)if(ch=A&ch=a&ch=0&ch=9)digiter+;elseother+;printf(letter=%d,digiter=%d,other=%d,letter,digiter,other);if(fclose(fp)printf(Cannotclosethefile!

n);exit(0);return0;2.将实数写入文件:

从键盘输人若干实数(以特殊数值-1结束),分别写到一个文本文件中。

试编写相应程序。

解答:

#include#includeintmain(void)FILE*fp;intnumber;if(fp=fopen(12-,w)=NULL)printf(cannotopenfile!

n);exit(0);printf(Inputnumber:

n);scanf(%d,&number);while(number!

=-1)fprintf(fp,%d,number);scanf(%d,&number);if(fclose(fp)printf(Cannotclosethefile!

n);exit(0);return0;3.比较两个文本文件是否相等:

比较两个文本文件的内容是否相同,并输出两个文件中第一次出现不同字符内容的行号及列值。

试编写相应程序。

解答:

#include#includeintmain(void)FILE*fp1,*fp2;inti=1,j=1;charch1,ch2;if(fp1=fopen(12-,r)=NULL)printf(cannotopenfile!

n);exit(0);if(fp2=fopen(12-,r)=NULL)printf(cannotopenfile!

n);exit(0);ch1=fgetc(fp1);ch2=fgetc(fp2);while(ch1!

=EOF&ch2!

=EOF)if(ch1!

=ch2)break;elseif(ch1!

=n)j+;ch1=fgetc(fp1);ch2=fgetc(fp2);elsei+;j=1;ch1=fgetc(fp1);ch2=fgetc(fp2);printf(首次不同的字符在第%d行、第%d列。

n,i,j);if(fclose(fp1)printf(cannotclosefile!

n);exit(0);if(fclose(fp2)printf(cannotclosefile!

n);exit(0);4.将文件中的数据求和并写入文本文件尾:

文件Int_Data.dat中存放了若干整数,将文件中所有数据相加,并把累加和写入该文件的最后。

试编写相应程序。

解答:

#include#includeintmain(void)FILE*fp;intx,sum=0;if(fp=fopen(Int_Date.dat,ab+)=NULL)printf(cannotopenfile!

);exit(0);while(!

feof(fp)fscanf(fp,%d,&x);sum+=x;fprintf(fp,%d,sum);if(fclose(fp)printf(cannotclosethefile!

n);exit(0);return0;5.输出含for的行:

将文本文件test.txt中所有包含字符串“for”的行输出。

试编写相应程序。

解答:

#include#include#includechars999;intmain(void)FILE*fp;inti;if(fp=fopen(test.txt,r)=NULL)printf(cannotopenfile!

n);exit(0);while(!

feof(fp)fgets(s,999,fp);if(strlen(s)3)for(i=0;istrlen(s)-2;i+)if(si=f&si+1=o&si+2=r)printf(%s,s);if(fclose(fp)printf(cannotclosefile!

n);exit(0);return0;6.删除文件中的注释:

将C语言源程序(hello.c)文件中的所有注释去掉后存入另一个文件(new_hello.c)。

试编写相应程序。

解答:

#include#includeintmain(void)FILE*fp1,*fp2;charch,ch1,ch2,s99;if(fp1=fopen(hello.c,r)=NULL)printf(cannotopenfile!

n);exit(0);if(fp2=fopen(new_hello.c,w)=NULL)printf(cannotopenfile!

n);exit(0);while(!

feof(fp1)ch=fgetc(fp1);if(ch=/)if(ch1=fgetc(fp1)=*)while(fgetc(fp1)!

=*&(ch1=fgetc(fp1)!

=/)fseek(fp1,-sizeof(ch1),1);elseif(ch1=/)ch=0;for(;ch1!

=n;ch1=fgetc(fp1)ch1=0;elseif(ch!

=EOF)fputc(ch,fp2);if(fclose(fp2)printf(cannotclosefile!

n);exit(0);return0;7.(选做)账户余额管理:

创建一个随机文件,用来存储银行账户和余额信息,程序要求能够查询某个账户的余额,当客户发生交易额时(正表示存入,负表示取出)能够更新余额。

账户信息包括账号、账号名和余额三个数据项。

试编写相应程序。

文件部分内容如下:

AcctNoAcctNameBalance1zhangsan1000.002lisi1300.003wangwu-100.00解答:

#include#include#includelongsize;structaccountcharno10;characctname50;doublebalance;FILE*openfile(char*openmode)FILE*fp;if(fp=fopen(accout.dat,openmode

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 临床医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1