更新时间:2022-08-13 14:29:09
返回:贺老师课程教学链接
【项目1-由键盘到文件】
(1)从键盘输入一个文件名,以及一个以#结束的字符序列,将输入的字符保存到文件中去。
#include "stdio.h" #include "stdlib.h" int main() { FILE *fp ; char ch,fname[10]; printf("文件名:"); gets(fname); if ((fp=____(1)____)==NULL) { printf("connot open\n"); exit(0); } while ((ch=getchar())!='#') fputc(____(2)____); ____(3)____; return 0; }
#include "stdio.h" #include "stdlib.h" int main() { FILE *fp1,*fp2 ; char c; if ((fp1=fopen("f1.dat", ___(1)___))==NULL) { printf("connot open\n"); exit(0); } if ((fp2=fopen("f2.dat", ___(2)___))==NULL) { printf("connot open\n"); exit(0); } c=fgetc(fp1); while (___(3)___) { fputc(c,fp2); c=fgetc(fp1); } ___(4)___ return 0; }
#include "stdio.h" #include "stdlib.h" int main() { FILE ___(1)___; char ch; fp1=fopen("file1.dat","r"); fp2=fopen("file2.dat","w"); while (!feof(fp1)) { ch=___(2)___; putchar(ch); fputc(___(3)___); } fclose(fp1); fclose(fp2) ; return 0; }[参考解答]
#include <stdio.h> #include <stdlib.h> int main() { int score; //读入的成绩 int excelent=0, fail=0,count=0;//分别代表优秀、不及格人数、总人数 double sum=0,ave; //sum: 成绩和,ave: 平均分 //以输入的方式(ios::in)打开文件 FILE ____(1)____; fp=fopen(____(2)____); if(fp==NULL) { printf("open error!\n"); exit(1); } while(fscanf(____(3)____)!=EOF) //当读取成功…… { count++; sum+=score; if(____(4)____) excelent++; else if(score<60) ____(5)____; } ____(6)____; //下面输出结果 ave=sum/count; printf("总人数为:%d\n", count); printf("平均成绩为:%.2f\n", ave); printf("优秀人数:%d\n", excelent); printf("不及格人数:%d\n", fail); return 0; }
【项目4-算工资】
从文件salary.txt中读入工人的工号、基本工资、奖金,将奖金全部增加20%(好事)后,将工号、基本工资、奖金和应发工资(前项目之和)保存到文件salarylist.txt中。
[参考解答]