C语言程序设计何钦铭-颜-晖-第12章--文件.pdf
《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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言程序设计 12 文件
限制150内