C语言程序设计(第3版)何钦铭颜晖第12章文件文档推荐

上传人:粗**** 文档编号:135281757 上传时间:2020-06-14 格式:PDF 页数:17 大小:25.65KB
返回 下载 相关 举报
C语言程序设计(第3版)何钦铭颜晖第12章文件文档推荐_第1页
第1页 / 共17页
C语言程序设计(第3版)何钦铭颜晖第12章文件文档推荐_第2页
第2页 / 共17页
C语言程序设计(第3版)何钦铭颜晖第12章文件文档推荐_第3页
第3页 / 共17页
C语言程序设计(第3版)何钦铭颜晖第12章文件文档推荐_第4页
第4页 / 共17页
C语言程序设计(第3版)何钦铭颜晖第12章文件文档推荐_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《C语言程序设计(第3版)何钦铭颜晖第12章文件文档推荐》由会员分享,可在线阅读,更多相关《C语言程序设计(第3版)何钦铭颜晖第12章文件文档推荐(17页珍藏版)》请在金锄头文库上搜索。

1、第 12章文件 练习 12 1 读出例 12 1 学生成绩文件内容 输出最高分和最低分及相应的学 号和姓名 解答 include include struct student long num char stname 20 int score int main void FILE fp int i max min j 0 k 0 struct student students 5 if fp fopen r NULL printf File open error n exit 0 fscanf fp ld s d max min students 0 score for i 1 i 4 i f

2、scanf fp ld s d if maxstudents i score min students i score k i printf Max score d num d name s n students j score students j num printf Min score d num d name s n students k score students k num if fclose fp printf Can not close the file n exit 0 return 0 练习 12 2 请使用例 8 9 答电码加密函数对民吗字符串进行加密 改写例 12 2

3、 解答 include include include struct sysuser char username 20 char password 8 void encrypt char pwd int main void FILE fp int i struct sysuser su if fp fopen w NULL printf File open error n exit 0 for i 1 i 5 i printf Enter dth sysuser name password i scanf s s encrypt fprintf fp s s n if fclose fp pr

4、intf Can not close the file n exit 0 return 0 void encrypt char pwd int i for i 0 i strlen pwd i if pwd i z pwd i a else pwd i 1 练习 12 3 例 12 3 中为什么在执行 fputc ch fp2 前要判断 ch 的值是否等 于 EOF改写例 12 3 的程序 在复制用户信息文件后 再统计被复制文件中字符 的数量 解答 文件结束符 EOF是一个值为 1 的常量 读文件时可用来判断从文件中读入的字 符是否为 EOF来决定循环是否继续 include include

5、int main void FILE fp1 fp2 char ch int 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 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

6、file n exit 0 printf f12 2 中字符数量为 d count return 0 练习 12 4 字母转换并统计行数 读取一个指定的文本文件 显示在屏幕上 如果有大写字母 则改成小写字母再输出 并根据回车符统计行数 试编写相应 程序 解答 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 else printf c ch if

7、 ch n countline printf n printf file s 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 printf can not open file exit 0

8、 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 实数取整写入文件 文件中有若干个实数 请分别读出 将每个实 数按四舍五入取整后存入文件中 试编写相应程序 解答 include include int main void FILE fp1 fp

9、2 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 fprintf fp2 0f a if fclose fp1 printf Can not close the file n exit 0 if fclose fp2 printf Can not close the file n exit 0 return 0 练习 12 7 修改例 12 6 增加修改资金账

10、户的功能 输入一个记录ID 如果文 件中已存在该记录 则输入新的记录信息并更新资金账户文件中相应记录的信 息 要求定义和调用函数UpdateLog 其功能是修改资金账户记录 解答 include include long size struct LogData long logid char logdate 11 char 1ognote 15 double charge double balance int inputchoice int mychoice printf nEnter your choice n printf 1 Add a new cash LOG n2 List All

11、Cash LOG n printf 3 Query Last Cash LoG n0 End program n scanf d return mychoice long getLogcount FILE cfptr long begin end logcount fseek cfptr OL SEEK SET begin ftell cfptr fseek cfptr size SEEK END end ftell cfptr logcount end begin size 1 return logcount 列出所有收支流水账 void ListAllLog FILE cfptr stru

12、ct LogData log fseek cfptr OL SEEK SET fread printf logid logdate lognote charge balance n while feof cfptr printf 6ld 11s 15 n fread 查询显示最后一条记录 void QueryLastLog FILE cfptr struct LogData log long logcount logcount getlogcount cfptr if 1ogcount 0 fseek cfptr size logcount 1 SEEK SET fread printf Th

13、e last log is n printf logid 6ld nlogdate 11s nlognote 15s n printf charge nbalance n else printf no logs in file n 添加新记录 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 epen

14、d scanf lf logcount getLogcount cfptr if logcount 0 fseek cfptr size logcount 1 SEEK SET fread else 1 rewind cfptr ogid last taraetlastlog printf logid ld n fwirte 修改资金账户 void UpdateLog FILE cfptr FILE fpout struct LogData user char date 11 char note 15 double charge double balance int choice ID cfp

15、tr fileopen r if fpout fopen w NULL printf can not open the file n exit 0 printf Enter LogID scanf d while feof cfptr fread if strcmp ID 0 printf 请输入修改信息 n printf Date scanf s date strcpy date printf Note scanf s note strcpy note printf Charge charge printf Balance scanf s balance fwrite else fwrite

16、 if fclose cfptr printf can not close file n exit 0 if fclose fpout printf can not close file n exit 0 unlink break if fclose fp printf Can 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 3 定义 F ILE fp 则文件指针 fp 指向的是 D A 文件在磁盘上的读写位置B文件在级冲区上的读写位置 C 整个磁盘文件D 文件类型结构 4 缓冲文件系统的文件缓冲区位于 C A 磁盘缓冲区中B 磁盘文件中 C 内存数据区中D 程序文件中 5 使文件指针重新定位到文件读写的首地址的函数是 C 二 填空题 1 函数 fopen 的返回值是 指向文件缓冲区的首地址的文件结构类型指针 2 文

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

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号