国产最新a级毛片无码专区_综合亚洲欧美日韩久久精品_日本成年片在线观看66_一本到九九av电影_一级毛片免费网站播放_国内精品久久人无码大片_国产人成视频99在线观看_欧美不卡在线一本二本_国产亚洲电影av_可以免费看黄色软件

知ing

大學(xué)C/C++語(yǔ)言程序設(shè)計(jì)實(shí)驗(yàn)教程

陽(yáng)小華 羅晨暉 編 / 電子工業(yè)出版社

王大膽 上傳

查看本書(shū)

實(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);

}

?


查看更多