本文主要是介绍第四十三课 从文本文件中读入数据 【项目1-4】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第四十三课 从文本文件中读入数据
项目一【由键盘到文件】
(1)从键盘输入一个文件名,以及一个以#结束的字符序列,将输入的字符保存到文件中去。
(2)设上题建立了名为f1.dat的文件,请将这个文件拷贝到一个名为f2.dat的文件中。
(3)以下程序的功能是将文件file1.dat的内容输出到屏幕上并复制到文件file2.dat中。
(1)代码:
#include "stdio.h"
#include "stdlib.h"
int main()
{FILE *fp ;char ch,fname[10];printf("文件名:");gets(fname);if ((fp=fopen(fname,"w"))==NULL){printf("connot open\n");exit(0);}while ((ch=getchar())!='#')fputc(ch,fp);fclose(fp);return 0;
}
#include<stdlib.h>
#include<stdio.h>
int main()
{FILE *fp1,*fp2;char c;if((fp1=fopen("f1.dat","r"))==NULL){printf("connot open\n");exit(0);}if((fp2=fopen("f2.dat","w"))==NULL){printf("connot open\n");exit(0);}c=fgetc(fp1);while(c!=EOF){fputc(c,fp2);c=fgetc(fp1);}fclose(fp1);fclose(fp2);return 0;
}
运行结果:
(3)代码:
#include<stdio.h>
#include<stdlib.h>
int main()
{FILE *fp1,*fp2;char ch;fp1=fopen("file1.dat","r");fp2=fopen("file2.dat","w");while(!feof(fp1)){ch=fgetc(fp1);putchar(ch);fputc(ch,fp2);}fclose(fp1);fclose(fp2);printf("\n");return 0;
}
运行结果:
项目二【文件中的符号个数】
统计一个文本文件中数字、空格、字母出现的次数,以及文件的字节数,并将结果输出,文本文件名在程序中输入(请自建文本文件完成测试)。
代码:
#include<stdio.h>
#include<stdlib.h>
int main()
{FILE *fp;char ch;int num=0,sp=0,wor=0,n=0;fp=fopen("test.txt","r");if(fp==NULL){printf("connot open\n");exit(0);}while((ch=fgetc(fp))!=EOF){n++;if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))wor++;else if(ch>='0'&&ch<='9')num++;else if(ch==' ')sp++;}printf("字母%d,数字%d,空格%d,字节%d\n",wor,num,sp,n);fclose(fp);return 0;
}
运行结果:
项目三【成绩统计】
文件 english.dat (这个文件中的数据量,超出了你之前所有的体验)中已经有了学生的英语考试成绩数据。
(1)请编程从english.dat中读取数据,求出这次考试的平均成绩,并统计输出优秀人数和不及格人数。请在下面程序基础上填空完成:
(2)扩充上面的程序,要求将统计结果保存到数据文件statictic.dat中(提示:要用fprintf写入文件了)
(3)(用柱状图输出)编程序,求出这次考试的平均成绩,并统计各分数段的人数(优秀:≥90,良好:≥80,中等:≥70,及格:≥60,不及格:<60)。运行结果如图所示。试着将输出界面改变成成下面的样子(仅变输出方式)
(1)请编程从english.dat中读取数据,求出这次考试的平均成绩,并统计输出优秀人数和不及格人数。请在下面程序基础上填空完成:
(2)扩充上面的程序,要求将统计结果保存到数据文件statictic.dat中(提示:要用fprintf写入文件了)
(3)(用柱状图输出)编程序,求出这次考试的平均成绩,并统计各分数段的人数(优秀:≥90,良好:≥80,中等:≥70,及格:≥60,不及格:<60)。运行结果如图所示。试着将输出界面改变成成下面的样子(仅变输出方式)
(1)代码:
#include<stdio.h>
#include<stdlib.h>
int main()
{int score;int excelent=0,fail=0,count=0;double sum=0,ave;FILE *fp;fp=fopen("english.dat","r");if(fp==NULL){printf("open error!\n");exit(1);}while(fscanf(fp,"%d",&score)!=EOF){count++;sum+=score;if(score>=90)excelent++;else if(score<60)fail++;}fclose(fp);ave=sum/count;printf("总人数为:%d\n",count);printf("平均成绩为:%.2f\n",ave);printf("优秀人数:%d\n",excelent);printf("不及格人数:%d\n",fail);return 0;
}
运行结果:
(2)代码:
#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 *fp,*sp;fp=fopen("english.dat","r");if(fp==NULL){printf("open error!\n");exit(1);}sp=fopen("statictic.dat","w");if(sp==NULL){printf("error\n");exit(1);}while(fscanf(fp,"%d",&score)!=EOF) //当读取成功……{count++;sum+=score;if(score>=90)excelent++;else if(score<60)fail++;}ave=sum/count;fprintf(sp,"总人数为:%d\n", count);fprintf(sp,"平均成绩为:%.2f\n", ave);fprintf(sp,"优秀人数:%d\n", excelent);fprintf(sp,"不及格人数:%d\n", fail);fclose(fp);fclose(sp);return 0;
}
运行结果:
(3)代码:
#include<stdio.h>
#include<stdlib.h>
int main()
{int score,r;int best=0,good=0,mid=0,passing=0,fail=0,count=0;double sum=0,ave;FILE *fp;fp=fopen("english.dat","r");if(fp==NULL){printf("error");exit(1);}while(fscanf(fp,"%d",&score)!=EOF){count++;sum=sum+score;r=score/10;switch(r){case 10:case 9:best++;break;case 8:good++;break;case 7:mid++;break;case 6:passing++;break;default:fail++;break;}}fclose(fp);ave=sum/count;printf("平均成绩 %.4f\n",ave);int l,i;printf("优秀 ");l=best/100;for(i=0;i<=l;i++)printf("X");printf(" %d",best);printf("\n良好 ");l=good/100;for(i=0;i<=l;i++)printf("X");printf(" %d",good);printf("\n中等 ");l=mid/100;for(i=0;i<=l;i++)printf("X");printf(" %d",mid);printf("\n及格 ");l=passing/100;for(i=0;i<=l;i++)printf("X");printf(" %d",passing);printf("\n挂科 ");l=fail/100;for(i=0;i<=l;i++)printf("X");printf(" %d",fail);printf("\n");return 0;
}
运行结果:
项目四【算工资】
从文件 salary.txt 中读入工人的工号、基本工资、奖金,将奖金全部增加20%(好事)后,将工号、基本工资、奖金和应发工资(前项目之和)保存到文件salarylist.txt中。
代码:
#include<stdio.h>
#include<stdlib.h>
int main()
{int num;double salary,award,sum;FILE *fp1,*fp2;fp1=fopen("salarylist.txt","r");if(fp1==NULL){printf("error");exit(1);}fp2=fopen("salary.txt","w");if(fp2==NULL){printf("error2");exit(1);}while(fscanf(fp1,"%d %lf %lf %lf",&num,&salary,&award,&sum)!=EOF){award*=1.2;fprintf(fp2,"%d %.2lf %.2lf\n",num,salary,award+salary);}fclose(fp1);fclose(fp2);printf("over!");return 0;
}
运行结果:
总结:
文件读写的语句和以前的输入输出不一样,要写对格式
读入文件的时候要记得判断指针函数的值,如果是NULL要提示打开失败
程序结束前要关闭文件
260
这篇关于第四十三课 从文本文件中读入数据 【项目1-4】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!