填空題下列程序?qū)㈡I盤(pán)輸入的若干姓名、成績(jī)寫(xiě)入文本文件,寫(xiě)入過(guò)程中文件中的數(shù)據(jù)均按成績(jī)從高到低存放。請(qǐng)?zhí)羁諏⒊绦蜓a(bǔ)充完整、正確。#include#includevoidmain(){charname[9],na[9];intscore,sc,i,pt1,pt2,flag;;FILE*p1;if((p1=fopen("d:\\file1.dat","w+"))==NULL){printf("文件不能打開(kāi)!\n");exit(0);}scanf("%s%d",name,&score);//讀入第一個(gè)人的姓名、成績(jī)并寫(xiě)入文件fprintf(p1,"%8s%3d\n",name,score);while(scanf("%s%d",name,&score)!=EOF){//讀入其他人的姓名、成績(jī)r(jià)ewind(p1);//將讀寫(xiě)位置移動(dòng)到文件頭while(fscanf(p1,"%8s%3d\n",na,&sc)!=EOF)if(score>sc){fseek(p1,–13,1);pt1=ftell(p1);//確定數(shù)據(jù)的插入位置;pt2=ftell(p1);//確定文件尾部位置for(i=pt2-13;i>=pt1;i=i-13){fseek(p1,i,0);fscanf(p1,"%8s%3d\n",na,&sc);fseek(p1,i+13,0);}fseek(p1,pt1,0);fprintf(p1,"%8s%3d\n",name,score);flag=0;//鍵盤(pán)輸入數(shù)據(jù)已插入,標(biāo)志位置0break;}if(){//若數(shù)據(jù)未插入,則在文件末尾插入//移動(dòng)文件讀寫(xiě)位置到文件末尾fprintf(p1,"%8s%3d\n",name,score);}}fclose(p1);}

您可能感興趣的試卷

你可能感興趣的試題

1.填空題文本文件a.dat、b.dat中每行存放一個(gè)數(shù)且均按從小到大存放。下列程序?qū)⑦@兩文件中數(shù)據(jù)合并到c.dat,文件c.dat中數(shù)據(jù)也要從小到大存放。請(qǐng)?zhí)羁諏⒊绦蜓a(bǔ)充完整、正確(若文件a.dat數(shù)據(jù)為1、6、9、18、27、35,文件b.dat數(shù)據(jù)為10、23、25、39、61,則文件c.dat中數(shù)據(jù)應(yīng)為1、6、9、10、18、23、25、27、35、39、61)。#include#includevoidmain(){FILE*f1,*f2,*f3;intx,y;if((f1=fopen("a.dat","r"))==NULL){printf("文件a.dat不能打開(kāi)!\n");exit(0);}if((f2=fopen("b.dat","r"))==NULL){printf("文件b.dat不能打開(kāi)!\n");exit(0);}if(()==NULL){printf("文件c.dat不能打開(kāi)!\n");exit(0);}fscanf(f1,"%d",&x);;while(!feof(f1)&&!feof(f2))if(){fprintf(f3,"%d\n",x);fscanf(f1,"%d",&x);}else{fprintf(f3,"%d\n",y);fscanf(f2,"%d",&y);}if(feof(f1)){;while(!feof(f2)){fscanf(f2,"%d",&y);;}}else{fprintf(f3,);while(!feof(f1)){;fprintf(f3,"%d\n",x);}}fclose(f1);fclose(f2);fclose(f3);}