實(shí)驗(yàn)13 ?文件的使用
任務(wù)1 ?文件的讀、寫(xiě)操作
1.建立一個(gè)名為abc的文件,并將從鍵盤(pán)輸入的字符存入該文件,當(dāng)鍵盤(pán)輸入結(jié)束時(shí)關(guān)閉該文件,填空完成下面程序。
#include ?<stdio.h>
main( )
{ ?FILE *fp;
???char ch;
???fp=??fopen ?("abc", ?"w" ?);
???do {
????????ch=getchar ( );
????????fputc ( ch, fp);
???} while (?ch!= EOF);
???fclose (fp);
}
2.從鍵盤(pán)輸入一個(gè)以“#”為結(jié)束標(biāo)志的字符串,將它存入指定的文件中,填空完成下面程序。
#include<stdio.h>
main( )
{
???FILE *fp;
???char ch,fn[10];
???printf("\nInput the file name: ");
???scanf("%s", fn);
???if( (??fp=fopen(fn,"w") ?)==NULL)
{
printf("\nCannot create file");
exit(1);
}
???ch=getchar( );
???while (??(ch=getchar())!='#' ?)
fputc(ch, fp);
??close(fp);
}
3.文件復(fù)制,文件名來(lái)自main(?)函數(shù),填空完成下面程序。
#include<stdio.h>
void ?file_copy(FILE *fout, FILE *fin)
{ ?char k;
???do{
????k=fgetc(??fin ?);
????if(feof(fin))
??break;
????fputc(??k,fout ?);
?????}while( 1 );
}
void ?main(int argc, char ?*argv[ ])
{ ?FILE ?*fp1, *fp2;
???if( argc !=3) ??return;
???if((fp1=fopen(argv[2], "rb"))==NULL) ???return;
??fp2=??fopen(argv[1],"wb") ?;
??file_copy (fp2,fp1);
??fclose(fp1);
??fclose(fp2);
}
?
任務(wù)2 ?文件綜合程序設(shè)計(jì)
設(shè)計(jì)創(chuàng)建原始成績(jī)表b1.txt,從文件b1.txt中取出成績(jī),排序后,按降序存放b2.TXT中,并輸出前五名成績(jī)報(bào)表。
參考代碼如下:
#include "stdio.h"
#define N 10
struct student
{
????int num;
????char name[20];
????int score[3]; ?
????float average;
};
void sort(struct student stu[]);
void print(FILE *fp, struct student stu[]);
void printtopfive(FILE *fp, struct student stu[]);
void main( )
{
????struct student s[N];
????int i;
????FILE *fp1,*fp2;
????char ch;
????if((fp1=fopen("c:\\b1.txt","r"))==NULL)
???????{
???????printf("不能打開(kāi)文件c:\\b1.txt");
???????exit(1);
???????}
????if((fp2=fopen("c:\\b2.txt","w"))==NULL)
???????{
???????printf("不能打開(kāi)文件c:\\b2.txt");
???????exit(1);
???????}
????for(i=0;i<N;i++)
???????{
??????????fscanf(fp1,"%d%s%d%d%d",&s[i].num,s[i].name,&s[i].score[0],
????????????????????&s[i].score[1],&s[i].score[2]);
??????????s[i].average=(s[i].score[0]+s[i].score[1]+s[i].score[2])/3.0;
???????}
????fprintf(fp2,"原始成績(jī)報(bào)表\n");
????print(fp2,s);
????sort(s);
????fprintf(fp2,"排序之后的成績(jī)報(bào)表\n");
????print(fp2,s);
????fprintf(fp2,"前五名成績(jī)報(bào)表\n");
????printtopfive(fp2,s);
}
void sort(struct student stu[])
{ ??int i,k,j;
????struct student t;
????for(i=0;i<N-1;i++)
????{ ??k=i;
????????for(j=i+1;j<N;j++)
??????????{ ??if(stu[k].average<stu[j].average)k=j; }
??????????????if(k!=i)
??????????{
??????????????t=stu[i];
??????????????stu[i]=stu[k];
??????????????stu[k]=t;
??????????}
???}
}
void print(FILE *fp, struct student stu[])
{
????int i;
????fprintf(fp,"StudentID ?StudentName ??Score1 ?Score2 ?Score3 ?Average\n");
????for(i=0;i<N;i++)
?????????fprintf(fp,"%-10d%-12s%8d%8d%8d%8.1f\n",stu[i].num,stu[i].name,
?????????stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].average);
}
void printtopfive(FILE *fp, struct student stu[])
{
????int i;
????fprintf(fp,"StudentName ??Average\n");
????for(i=0;i<5;i++)
?????????fprintf(fp,"%-12s%8.1f\n",stu[i].name,stu[i].average);
}
?