这个题目太简单了你可以参考我的代码
复制程式
#include<stdio.h>
#include <conio.h>
#include<string.h>
#include <stdlib.h>
/*define the number of the student as 10*/
#define num 10
/*struct Student definition*/
struct Student
{
char number[5];
int score;
}stuscore[num];
/*definition end*/
/*function declared*/
void SortStudentScore(struct Student stu[num]);
void ReadStudentScore(char *fileName,struct Student stu[num]);
void WriteStudentScore(char *fileName,struct Student stu[num]);
/*declare end*/
int main(void)
{
char * sourcefile="S1_Score.txt";
char * targetfile="1_OrderScore.txt";
ReadStudentScore(sourcefile,stuscore);
SortStudentScore(stuscore);
WriteStudentScore(targetfile,stuscore);
return 0;
}
void ReadStudentScore(char *fileName,struct Student stu[num])
{
FILE * fp;
int i=0;
struct Student * getchara=stu;
if((fp=fopen(fileName,"r"))==NULL)
{
printf("Error!can't open the source file %s\n!",fileName);
getch();
exit(1);
}
for(i=0;i<num;i++)
{
fscanf(fp,"%5s",getchara->number);
fgetc(fp);
fscanf(fp,"%d\n",&getchara->score);
getchara++;
}
fclose(fp);
}
/*the followed SortStudentScore is used to sorts the student.*/
/*SortStudentScore is completed by the 'maopao' method*/
void SortStudentScore(struct Student stu[num])
{
int i,j,flag;
struct Student temp;
for(i=0;i<num-1;i++)
{
flag=0;
for(j=num-1;j>i;j--)
if(stu[j].score>stu[j-1].score)
{
temp=stu[j];
stu[j]=stu[j-1];
stu[j-1]=temp;
flag=1;
}
if(flag==0)
break;
}
}
/*SortStudentScore function end*/
void WriteStudentScore(char *fileName,struct Student stu[num])
{
int i;
FILE *fp=fopen(fileName,"r");
for(i=0;i<num;i++)
{
fscanf(fp,"%s%d",stu[i].number,&stu[i].score);
}
fclose(fp);
}