




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验12 文件程序设计实验目的1)掌握文件的基本概念2)掌握文本文件的打开和关闭操作3)掌握文本文件的顺序读,写方法实验内容1、基础编程题(2)将实数写入文件:从键盘输入若干实数(以特殊数值-1结束),分别写到一个文本文件中。试编写相应程序。2.1程序源代码#include int main(void) int n; FILE *pf = fopen(1.txt, w+); if (!pf) puts(open file error!); return -1; while (true) printf(input a num:); scanf(%d, &n); if (n=-1) break; fprintf(pf, %d , n); fclose(pf); puts(ok!); return 0;2.2运行结果(3)统计成绩:从键盘输入以下10个学生的学号,姓名,以及数学,语文和英语成绩,写到文本文件f3.txt中,再从文件中取出数据,计算每个学生的总成绩和平均分,并将结果显示在屏幕上。试编写相应程序。3.1程序源代码#include#include#includevoid test2();int main()test2();struct student long no; char name20; int math; int chinese; int english; int sum; double ave;void test2() struct student student_1;FILE *fp = NULL; char buff1000=学号t姓名t数学t语文t英语t总成绩t平均分; int i; fp = fopen(f3.txt, w+); fputs(buff,fp); fputs(n,fp); for(i=0;i10;i+) scanf(%ld %s %d %d %d,&student_1.no,student_1.name,&student_1.math,&student_1.chinese,&student_1.english); student_1.sum=student_1.math+student_1.chinese+student_1.english; student_1.ave=student_1.sum/3; fprintf(fp,%ldt%st%dt%dt%dt%dt%1.0lfn,student_1.no,student_1.name,student_1.math,student_1.chinese,student_1.english,student_1.sum,student_1.ave); printf(%sn,buff); fseek(fp,sizeof(buff),SEEK_SET); for(i=0;i10;i+) fscanf(fp,%ldt%st%dt%dt%dt%dt%1.0lfn,&student_1.no,student_1.name,&student_1.math,&student_1.chinese,&student_1.english,&student_1.sum,&student_1.ave); printf(%ldt%st%dt%dt%dt%dt%1.0lfn,student_1.no,student_1.name,student_1.math,student_1.chinese,student_1.english,student_1.sum,student_1.ave); fclose(fp); 3.2运行结果2、 改错题将文件中的数据求和并写入文本文件尾:文件Int_Data.dat中存放了若干整数,将文件中所有数据相加,并把累加和写入该文件的最后。2.1程序源代码#include#includeint main(void) FILE fp; int n,sum; if(fp=fopen(Int_Data.dat,r)=NULL) printf(Cant Open File!); exit(0);while(fscanf(fp,%d,&n)=EOF) sum=sum+n; fprintf(fp,%d,sum); fclose(fp); return 0;2.2运行结果2.3 实验过程将源代码改为:#include#includeint main()FILE *fp;int n,sum = 0;if(fp=fopen(int_data.dat,a+)=NULL)printf(Cant Open File!);exit(0);while(fscanf(fp,%d,&n) != EOF)sum=sum+n;fprintf(fp, %d,sum);fclose(fp);return 0;运行结果为:3、拓展编程(2)删除文件中的注释:将C语言源程序(hello.c)文件中的所有注释去掉后存入另一个文件(new_hello.c).试编写相应程序。2.1程序源代码#include#include#includevoid test4();int main()test4();void test4()FILE *fp = NULL;FILE *fpcp = NULL;char ch; if(fp = fopen(hello.c,r)=NULL) printf(文件不存在); exit(0); ; fpcp=fopen(new_hello.c,w); while(!feof(fp) ch=fgetc(fp); if(ch=/) if(fgetc(fp)=*) while(fgetc(fp)!=/) continue; else if(ch!=EOF) fputc(ch,fpcp); fclose(fp); fclose(fpcp);2.2运行结果(3)账户余额管理:创建一个随机文件,用来存储银行账户和余额信息,程序要求能够查询某个账户的余额,当客户发生交易额时(正表示存入,负表示取出),并能更新余额。账户信息包括:账号,账号名和余额3个数据项。试编写相应程序3.1程序源代码#include #include#include long size;struct account char no10; char acctname50; double balance;FILE *openfile(char *openmode);double userbalance(FILE *fp,char *name);void pay(FILE *fp,char *name,double count);int main() FILE *fp; int choice; char name50; double balance; double count; printf(请输入选择类型n); printf(1.查账户余额n); printf(2.账户交易n); printf(退出按exitn); scanf(%d,&choice); switch(choice) case 1: printf(输入名字); scanf(%s,name); fp=openfile(r+); balance=userbalance(fp,name); printf(%.2lf,balance); break; case 2: printf(输入名字); scanf(%s,name); fp=openfile(r+); printf(输入交易金额); scanf(%lf,&count); pay(fp,name,count); break; default: exit(0); break; FILE *openfile(char *openmode) FILE *fp; if(fp=fopen(accout.dat,openmode)=NULL) printf(cant open); exit(0); return fp;double userbalance(FILE *fp,char *name) struct account user; double balance; fseek(fp,0L,SEEK_SET); while(!feof(fp) fscanf(fp,%s %s %lf,user.no,user.acctname,&user.balance); if(strcmp(user.acctname, name) = 0) balance=user.balance; break; return balance;void pay(FILE *fp,char *name,double count) FILE *fpout; struct account user; double balance; balance=userbalance(fp,name); balance=balance+count; fseek(fp,0L,SEEK_SET); fpout=fopen(temp.dat,w); while(!feof(fp) fscanf(fp,%s %s %lf,user.no,user.acctname,&user.balance); if(strcmp(user.acctname, name) = 0) user.balance=balance; /printf(%s %s %lf,user.no,user.acctname,user.balance); fprintf(fpout,%s %s %lfn,use
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024鄂州职业大学单招《物理》模拟试题含答案详解【培优】
- 2025年江苏省靖江市中考数学强化训练附答案详解AB卷
- 2025年连锁加盟合作细则合同版B版
- 2025年度车辆抵押权人责任保险合同
- 2025年甘肃省甘南州夏河县人民医院招聘专业技术人员15人笔试备考试题完整答案详解
- 2024-2025学年度专升本预测复习及参考答案详解(典型题)
- 智慧树知道网课《外科护理学(宁夏医科大学)》课后章节测试答案
- 2025年电焊工操作技能比武考试试题(附答案)
- 2024-2025学年度广播电视编辑记者复习提分资料及答案详解【名校卷】
- 2025年人际关系与沟通技巧能力考试试卷及答案
- DL∕T 2487-2022 电力燃煤机械名词术语
- 藏餐培训前台课程设计
- 对外投资合作国别(地区)指南 -玻利维亚-20240530-00504
- 19S406建筑排水管道安装-塑料管道
- 沪教版九年级上册化学第三章《物质构成的奥秘》检测卷(含答案解析)
- 如何与客户建立有效的沟通
- 薯片加工项目规划设计方案
- 复方电解质醋酸钠葡萄糖注射液-药品临床应用解读
- 变压器租赁协议书x
- 部编版小学数学六年级上册分数乘法应用题解法一:找单位“1”解析同步练习
- 危重产科患者麻醉管理
评论
0/150
提交评论