C语言程序设计何钦铭-颜-晖-第12章--文件.pdf

上传人:l**** 文档编号:82023720 上传时间:2023-03-24 格式:PDF 页数:34 大小:899.14KB
返回 下载 相关 举报
C语言程序设计何钦铭-颜-晖-第12章--文件.pdf_第1页
第1页 / 共34页
C语言程序设计何钦铭-颜-晖-第12章--文件.pdf_第2页
第2页 / 共34页
点击查看更多>>
资源描述

《C语言程序设计何钦铭-颜-晖-第12章--文件.pdf》由会员分享,可在线阅读,更多相关《C语言程序设计何钦铭-颜-晖-第12章--文件.pdf(34页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、 C 语 言 程 序 设 计(第 3 版)何 钦 铭-颜-晖-第 1 2 章-文 件(总 1 7 页)-本页仅作为文档封面,使用时请直接删除即可-内页可以根据需求调整合适字体及大小-2 第 12 章 文件 【练习 12-1】读出例 12-1 学生成绩文件内容,输出最高分和最低分及相应的学号和姓名。解答:#include#include struct student long num;char stname20;int score;int main(void)FILE*fp;int i,max,min,j=0,k=0;struct student students5;if(fp=fopen(,r

2、)=NULL)printf(File open error!n);exit(0);3 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(Max score:%d,num:%d,name:%sn,

3、studentsj.score,studentsj.num,&studentsj.stname);printf(Min score:%d,num:%d,name:%sn,studentsk.score,studentsk.num,&studentsk.stname);if(fclose(fp)printf(Can not close the file!n);4 exit(0);return 0;【练习 12-2】请使用例 8-9 答电码加密函数对民吗字符串进行加密,改写例12-2。解答:#include#include#include struct sysuser char username2

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

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

6、count=0;if(fp1=fopen(,r)=NULL)printf(File open error!n);exit(0);if(fp2=fopen(,w)=NULL)printf(File open error!n);exit(0);7 while(!feof(fp1)ch=fgetc(fp1);if(ch!=EOF)fputc(ch,fp2);count+;if(fclose(fp1)printf(Can not close the file!n);exit(0);if(fclose(fp2)printf(Can not close the file!n);exit(0);printf

7、(f12-2 中字符数量为:%d,count);return 0;【练习 12-4】字母转换并统计行数:读取一个指定的文本文件,显示在屏幕上,如果有大写字母,则改成小写字母再输出,并根据回车符统计行数。试编写相应程序。8 解答:#include#include int 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-A+a);else

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

9、)printf(can not open file!);exit(0);10 printf(Input the string: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(can not close file!n);exit(0);return 0;【练习 12-6】实数取整写入文件:文件中有若干个实数,请分别读出,将每个实数按四舍五入取整后存入文件中

10、。试编写相应程序。解答:11#include#include int main(void)FILE*fp1,*fp2;double a;if(fp1=fopen(,r)=NULL)printf(File open error!n);exit(0);if(fp2=fopen(,w)=NULL)printf(File open error!n);exit(0);while(!feof(fp1)fscanf(fp1,%lf,&a);fprintf(fp2,%.0f,a);if(fclose(fp1)printf(Can not close the file!n);exit(0);12 if(fclo

11、se(fp2)printf(Can not close the file!n);exit(0);return 0;【练习 12-7】修改例 12-6,增加修改资金账户的功能。输入一个记录 ID,如果文件中已存在该记录,则输入新的记录信息并更新资金账户文件中相应记录的信息。要求定义和调用函数 UpdateLog(),其功能是修改资金账户记录。解答:#include#include long size;struct LogData long logid;char logdate11;char 1ognote15;double charge;double balance;int inputchoic

12、e()13 int mychoice;printf(nEnter your choice:n);printf(1-Add a new cash LOG.n2-List All Cash LOG.n);printf(3-Query Last Cash LoG.n0-End program.n);scanf(%d,&mychoice);return mychoice;long getLogcount(FILE*cfptr)long begin,end,logcount;fseek(cfptr,OL,SEEK_SET);begin=ftell(cfptr);fseek(cfptr,size,SEEK

13、_END);end=ftell(cfptr);logcount=(end-begin)/size-1;return logcount;/*列出所有收支流水账*/void ListAllLog(FILE*cfptr)struct LogData log;fseek(cfptr,OL,SEEK_SET);14 fread(&log,size,1,cfptr);printf(logid logdate lognote charge balance n);while(!feof(cfptr)printf(%6ld%-11s%-15%n,;fread(&log,size,1,cfptr);/*查询显示最

14、后一条记录*/void QueryLastLog(FILE*cfptr)struct LogData log;long logcount;logcount=getlogcount(cfptr);if(1ogcount0)fseek(cfptr,size*(logcount-1),SEEK_SET);fread(&log,size,1,cfptr);printf(The last log is:n);printf(logid:%-6ldnlogdate:%-11snlognote:%-15sn,;printf(charge:%nbalance:n,;else printf(no logs in

15、file!n);15 /*添加新记录*/void AddNewLog(FILE*cfptr)struct LogData log,lastlog;long logcount;printf(Input logdate(format:2006-01-01):);scanf(%s,;printf(Input lognote:);scanf(%s,;printf(Input Charge:Income+and epend-:);scanf(%lf,&;logcount=getLogcount(cfptr);if(logcount0)fseek(cfptr,size*(logcount-1),SEEK_

16、SET);fread(&lastlog,size,1,cfptr)=+1;=+;else =1;=;16 rewind(cfptr);ogid=last-taraetlastlog;printf(logid=%ldn,;fwirte(&log,sizeof(struct LogData),1,cfptr);/*修改资金账户*/void UpdateLog(FILE*cfptr)FILE*fpout;struct LogData user;char date11;char note15;double charge;double balance;int choice,ID;cfptr=fileop

17、en(r);if(fpout=fopen(,w)=NULL)printf(can not open the file!n);exit(0);printf(Enter LogID:);scanf(%d,&ID);while(!feof(cfptr)17 fread(&user,sizeof(struct LogData),1,cfptr);if(strcmp,ID)=0)printf(请输入修改信息:n);printf(Date:);scanf(%s,date);strcpy,date);printf(Note:);scanf(%s,note);strcpy,note);printf(Charg

18、e:);=charge;printf(Balance:);scanf(%s,&balance);=balance;fwrite(&user,sizeof(struct LogData),1,fpout);else fwrite(&user,sizeof(struct LogData),1,fpout);if(fclose(cfptr)printf(can not close file!n);exit(0);18 if(fclose(fpout)printf(can not close file!n);exit(0);unlink();break;if(fclose(fp)printf(Can

19、not close the file!n);exit(0);return 0;习题 12 一、选择题 1.以下语句将输出_B_。#include printf(%d%d%d,NULL,0,EOF);0 1 0-1 EOF 0 EOF 2.如果二进制文件 已经存在,现在要写入全新数据,应以_B_方式打开。A.w B.wb C.w+D.“wb+19 3.定义”FILE *fp;,则文件指针 fp 指向的是_D_。A.文件在磁盘上的读写位置 B 文件在级冲区上的读写位置 C.整个磁盘文件 D.文件类型结构 4.缓冲文件系统的文件缓冲区位于_C_。A.磁盘缓冲区中 B.磁盘文件中 C.内存数据区中 D

20、.程序文件中 5.使文件指针重新定位到文件读写的首地址的函数是_C_。()()()()二、填空题 1.函数 fopen()的返回值是_指向文件缓冲区的首地址的文件结构类型指针_。2.文件的三大特征是_名称_、_大小_和_类型_。3.缓冲文件系统与非缓冲文件系统的不同点在于_系统是否为文件自动分配一块文件缓冲区(内存单元)_。4.只能向指定文件写入一个字符的函数是_fputc()函数_。5.判断文件指针是否已经到了文件尾部的函数是_feof()函数_。6.阅读程序,以下程序完成的功能是_文件 infile 内容复制到文件_。#include int main(void)char infile 1

21、0,outfile 10;FILE*fpa,*fpb;gets(infile);20 gets(outfile);fpa=fopen(infile,r);fpb=fopen(outfile,w);while(!feof(fpa)fputc(fgetc(fpa),fpb);fclose(fpa);fclose(fpb);return 0;三、程序设计题 1.统计文本文件中各类字符个数:分别统计一个文本文件中字母、数字及其他字符的个数。试编写相应程序。解答:#include#include int main(void)FILE*fp;char ch;int letter=0,digiter=0,o

22、ther=0;if(fp=fopen(,r)=NULL)printf(File open error!n);21 exit(0);while(ch=fgetc(fp)!=EOF)if(ch=A&ch=a&ch=0&ch=9)digiter+;else other+;printf(letter=%d,digiter=%d,other=%d,letter,digiter,other);if(fclose(fp)printf(Can not close the file!n);exit(0);return 0;2.将实数写入文件:从键盘输人若干实数(以特殊数值-1 结束),分别写到一个文本文件中。试

23、编写相应程序。解答:#include 22#include int main(void)FILE*fp;int number;if(fp=fopen(,w)=NULL)printf(can not open file!n);exit(0);printf(Input number:n);scanf(%d,&number);while(number!=-1)fprintf(fp,%d,number);scanf(%d,&number);if(fclose(fp)printf(Can not close the file!n);exit(0);return 0;23 3.比较两个文本文件是否相等:比

24、较两个文本文件的内容是否相同,并输出两个文件中第一次出现不同字符内容的行号及列值。试编写相应程序。解答:#include#include int main(void)FILE*fp1,*fp2;int i=1,j=1;char ch1,ch2;if(fp1=fopen(,r)=NULL)printf(can not open file!n);exit(0);if(fp2=fopen(,r)=NULL)printf(can not open file!n);exit(0);ch1=fgetc(fp1);ch2=fgetc(fp2);while(ch1!=EOF&ch2!=EOF)if(ch1!=

25、ch2)24 break;else if(ch1!=n)j+;ch1=fgetc(fp1);ch2=fgetc(fp2);else i+;j=1;ch1=fgetc(fp1);ch2=fgetc(fp2);printf(首次不同的字符在第%d 行、第%d 列。n,i,j);if(fclose(fp1)printf(can not close file!n);exit(0);if(fclose(fp2)printf(can not close file!n);exit(0);25 4.将文件中的数据求和并写入文本文件尾:文件 中存放了若干整数,将文件中所有数据相加,并把累加和写入该文件的最后。试

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

27、include#include char s999;int main(void)FILE*fp;int i;if(fp=fopen(,r)=NULL)printf(can not open file!n);exit(0);while(!feof(fp)fgets(s,999,fp);27 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(can not close file!n);exit(0);return 0;6.删除文件中的注释:将 C 语言源程序 文件

28、中的所有注释去掉后存入另一个文件。试编写相应程序。解答:#include#include int main(void)FILE*fp1,*fp2;char ch,ch1,ch2,s99;28 if(fp1=fopen(,r)=NULL)printf(can not open file!n);exit(0);if(fp2=fopen(,w)=NULL)printf(can not open file!n);exit(0);while(!feof(fp1)ch=fgetc(fp1);if(ch=/)if(ch1=fgetc(fp1)=*)while(fgetc(fp1)!=*&(ch1=fgetc

29、(fp1)!=/)fseek(fp1,-sizeof(ch1),1);else if(ch1=/)ch=0;for(;ch1!=n;ch1=fgetc(fp1)ch1=0;else if(ch!=EOF)fputc(ch,fp2);29 if(fclose(fp2)printf(can not close file!n);exit(0);return 0;7.(选做)账户余额管理:创建一个随机文件,用来存储银行账户和余额信息,程序要求能够查询某个账户的余额,当客户发生交易额时(正表示存入,负表示取出)能够更新余额。账户信息包括账号、账号名和余额三个数据项。试编写相应程序。文件部分内容如下:Ac

30、ctNo AcctName Balance 1 zhangsan 1 2 lisi 1 3 wangwu 解答:#include#include#include 30 long size;struct account char no10;char acctname50;double balance;FILE*openfile(char*openmode)FILE*fp;if(fp=fopen(,openmode)=NULL)printf(Can not open the file!);exit(0);return fp;double userbalance(FILE*fp,char*name)

31、;void pay(FILE*fp,char*name,double count);int main()FILE*fp;int choice;char name50;double balance;31 double count;printf(请输入选择类型:n);printf(1.查账户余额n);printf(2.账户交易n);printf(退出按 exitn);printf(选择类型:);scanf(%d,&choice);switch(choice)case 1:printf(输入名字);scanf(%s,name);fp=openfile(r+);balance=userbalance(

32、fp,name);printf(%.2f,balance);break;case 2:printf(输入名字);scanf(%s,name);fp=openfile(r+);printf(输入交易金额);scanf(%lf,&count);pay(fp,name,count);32 break;default:exit(0);break;return 0;double userbalance(FILE*fp,char*name)struct account user;double balance;rewind(fp);while(!feof(fp)fscanf(fp,%s%s%lf,&;if(

33、strcmp,name)=0)balance=;break;return balance;33 void pay(FILE*fp,char*name,double count)FILE*fpout;struct account user;double balance;balance=userbalance(fp,name);balance=balance+count;rewind(fp);fpout=fopen(,w);while(!feof(fp)fscanf(fp,%s%s%lf,&;if(strcmp,name)=0)=balance;fprintf(fpout,%s%s%lfn,;else fprintf(fpout,%s%s%lfn,;if(fclose(fp)printf(can not close file!n);exit(0);if(fclose(fpout)34 printf(can not close file!n);exit(0);unlink();/remove();都是删除文件的函数 rename(,);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 解决方案

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com