100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > c语言中成绩从文件中读入 C语言:从文件中读入数据

c语言中成绩从文件中读入 C语言:从文件中读入数据

时间:2021-03-22 06:27:38

相关推荐

c语言中成绩从文件中读入 C语言:从文件中读入数据

任务代码:

编程序:实现文本文件的复制

将source.txt文件复制成target.txt文件

#include #include int main()

{

printf("此程序执行将source.txt中的文本复制到target.txt之中!\n");

FILE *fpin,*fpout;//定义两个文件指针,因为要对两个文件实施操作

char c;

//打开和错误判断

if((fpin=fopen("source.txt","r"))==NULL)

{

printf("source file cannot open!");

exit(0);

}

if ((fpout=fopen("target.txt", "w"))==NULL)

{

printf("target file cannot open!");

exit(0);

}

//复制操作

while((c=fgetc(fpin))!=EOF)

{

fputc(c,fpout);

}

printf("finish copy!");

//关闭文件

fclose(fpin);

fclose(fpout);

return 0;

}

大奖赛分数:

#include #include int main()

{

printf("此程序执行将大奖赛的计分结果直接存入文件\n\n");

FILE *fp;//文件指针

//打开和错误判断,最终用户输入的数据保存到record.txt的文件之中

if ((fp=fopen("record.txt", "w"))==NULL)

{

printf("target file cannot open!");

exit(0);

}

//变量定义

int iJudger_Number,iSinger_SumNum,j,iCurr_SinggerNum;

float fMark,fAver,fSum,fMax,fMin;

printf("请输入评委总数和歌手总数:");//输入评委总数和歌手总数

scanf("%d %d",&iJudger_Number,&iSinger_SumNum);

//文件中打出表头

fprintf(fp,"No.\t");

for(j=1;j<=iJudger_Number;j++)

{

fprintf(fp,"评委%d\t",j);

}

fprintf(fp,"Max\tMin\tAve\n");

//用户输入比赛选手信息

iCurr_SinggerNum=1;

while(iCurr_SinggerNum<=iSinger_SumNum)

{

fSum=0;

fMax=0;

fMin=10;

j=1;

printf("参赛者%d:",iCurr_SinggerNum);

fprintf(fp,"%d\t",iCurr_SinggerNum);//在文件中打出No下面的数字,第几个参赛选手

do

{

scanf("%f",&fMark);

fprintf(fp, "%.2f\t", fMark);

fSum=fSum+fMark;

if(fMaxfMark)

{

fMin=fMark;

}

j++;

}while(j<=iJudger_Number);

fAver=(fSum-fMax-fMin)/(iJudger_Number-2); //计算平均成绩

fprintf(fp,"%.2f\t%.2f\t%.2f\t\n",fMax,fMin,fAver);//输出后三项

iCurr_SinggerNum++;

}

//关闭文件

fclose(fp);

return 0;

}

从一个11评委的文件读取分数并写入一个新的文件:

#include #include int main()

{

printf("此程序执行将大奖赛的计分结果直接存入文件\n\n");

FILE *fp1,*fp2;//文件指针

//文件指针及判断

fp1=fopen("score.dat","r");

if(fp1==NULL)

{

printf("file1 cannot open!");

exit(0);

}

fp2=fopen("target.csv","w");

if(fp2==NULL)

{

printf("file2 cannot open!");

exit(0);

}

//实际操作

int iJudge_Num=11,j,s;

float mark,max,min,ave,sum;

fprintf(fp2,"NO,MAX,MIN,AVE\n");//表头,必须要用,隔开

while(fscanf(fp1,"%d",&s)==1)//!!代表是还可以可以读到歌手的数据,也就是歌手数据还没有读完

{

sum=0;max=0;min=10;

fprintf(fp2,"%d,",s);//将歌手编号在输入目标文件的NO下必须记得打“,”

printf("calculate %d ....\n",s);

j=1;

do

{

fscanf(fp1,"%f",&mark);

sum=sum+mark;

if(maxmark)min=mark;

j++;

}while(j<=iJudge_Num);

ave=(sum-max-min)/(iJudge_Num-2);

fprintf(fp2,"%.2f,%.2f,%.2f\n",max,min,ave);//必须要用“,”号隔开,这样才能在csv格式中识别

}

//关闭指针

fclose(fp1);

fclose(fp2);

return 0;

}

实践1:从键盘输入一个文件名,以及一个以#结束的字符序列,将输入的字符保存到文件中去

#include #include int main()

{

printf("此程序执行从键盘输入一个文件名,以及一个以#结束的字符序列,将输入的字符保存到文件中去。\n");

FILE *fp;

char ch,fname[10];

printf("文件名:");

gets(fname);

if((fp=fopen(fname,"w"))==NULL)

{

printf("cannot open!\n");

exit(0);

}

while((ch=getchar())!='#')

{

fputc(ch,fp);

}

fclose(fp);

return 0;

}

实践2:设上题建立了名为f1.dat的文件,请将这个文件拷贝到一个名为f2.dat的文件中。

#include #include int main()

{

printf("此程序执行从设上题建立了名为f1.dat的文件,请将这个文件拷贝到一个名为f2.dat的文件中。\n");

FILE *fp1,*fp2;

char c;

//文件打开判断

fp1=fopen("f1.dat","r");

if(fp1==NULL)

{

printf("file1 cannot be open !");

exit(0);

}

fp2=fopen("f2.dat","w");

if(fp2==NULL)

{

printf("file 2 cannot be open !");

exit(0);

}

//复制操作

while ((c=fgetc(fp1))!=EOF) //(3):或者!feof(fp1),feof函数用于检查是否到达文件尾

{

fputc(c,fp2);

}

/*

c=fgetc(fp1);

while (c!=EOF)

{

fputc(c,fp2);

}

*/

//关闭指针文件

fclose(fp1);

fclose(fp2);

return 0;

}

实践3:

#include #include int main()

{

printf("此程序执行将文件file1.dat的内容输出到屏幕上并复制到文件file2.dat中,请补充完整。\n");

FILE *fp1,*fp2;

char ch;

//指针判断

fp1=fopen("file1.dat","r");

fp2=fopen("file2.dat","w");

if(fp1==NULL)

{

printf("file cannot open !");

exit(0);

}

if(fp2==NULL)

{

printf("file2 cannot open!");

exit(0);

}

//执行过程

while (!feof(fp1))

{

ch=fgetc(fp1);

putchar(ch);

fputc(ch,fp2);

}

fclose(fp1);

fclose(fp2) ;

return 0;

}

执行情况:

代码1执行之前:

代码2:

代码3:

实践1:

知识总结:

在输入csv文件的时候切记要用逗号将数据隔开,否则c语言无法识别将会造成毁灭性错误

心得体会:

在输入csv文件的时候切记要用逗号将数据隔开,否则c语言无法识别将会造成毁灭性错误

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。