已经有一个学生信息文本想根据他们的总绩点计算排名,但是出错了
void all_caculate_rank(int n) {
struct student_information st[100];
int i,j,temp,count=0;
FILE *fp;
fp=fopen("学生信息.txt","r");
if(fp==NULL)
{
printf("打开文件失败!\n");
getch();
exit(0);
}
fseek(fp,0L,SEEK_SET);
for(i=0;i<n;i++){
count = fscanf(fp,"%s\t%s\t%s\t%c\t%s\t%f %f %f %f %f %f %f %d\n",st[i].id,st[i].name,st[i].major,&st[i].graduate,st[i].birthday,&st[i].gaoshu,&st[i].lisan,&st[i].luoji,&st[i].gpa_gaoshu,&st[i].gpa_lisan,&st[i].gpa_luoji,&st[i].gpa,&st[i].rank);
if(count != 13){ // 检查读取文件时的错误
printf("读取文件失败!\n");
printf("%d",count);
getch();
exit(0);
}
}
fclose(fp);
//计算排名并存储
for(i=0;i<n;i++)
{
temp=1;//第1名
for(j=0;j<n;j++){
if(st[i].gpa<st[j].gpa) temp++;//如果较小,排名下降1名
}
st[i].rank=temp;
}
//更新记录
fp=fopen("全部排名后学生信息.txt","w");
for(i=0;i<n;i++)
{
fprintf(fp,"%s\t%s\t%s\t%c\t%s\t%f %f %f %f %f %f %f %d\n",st[i].id,st[i].name,st[i].major,st[i].graduate,st[i].birthday,st[i].gaoshu,st[i].lisan,st[i].luoji,st[i].gpa_gaoshu,st[i].gpa_lisan,st[i].gpa_luoji,st[i].gpa,st[i].rank);
}
fclose(fp);
printf("数据重新计算成功\n");
getch();
}
提示读取文件失败并且count 值为-1.,我的“"全部排名后学生信息”文件为空文件,学生信息文件为已有数据文件,内容如下:
20220001 leo computer N 2003-04-18 80.00 80.00 80.00 3.00 3.00 3.00 9.00 0
20220002 erin english N 2004-06-27 60.00 78.00 87.00 0.00 2.80 3.70 6.50 0
20220003 marry computer N 2004-09-23 80.00 90.00 80.00 3.00 4.00 3.00 10.00 0
20220004 bore computer N 2004-05-05 87.00 88.00 76.00 3.70 3.80 2.60 10.10 0
20220005 jerry computer N 2004-05-06 60.00 60.00 60.00 0.00 0.00 0.00 0.00 0
20220006 wade computer N 2004-09-09 50.00 50.00 50.00 0.00 0.00 0.00 0.00 0
20220007 nora english N 2003-08-07 87.00 88.00 67.00 3.70 3.80 1.70 9.20 0
20220008 hearth english N 2004-04-04 88.00 67.00 56.00 3.80 1.70 0.00 5.50 0
20220009 warmth english N 2004-06-06 87.00 68.00 88.00 3.70 1.80 3.80 9.30 0
20220010 hot english N 2003-03-03 88.00 34.00 76.00 3.80 0.00 2.60 6.40 0
20220011 cool english N 2003-01-01 88.00 77.00 66.00 3.80 2.70 1.60 8.10 0
20220012 cold computer N 2004-10-10 77.00 88.00 88.00 2.70 3.80 3.80 10.30 0
20220013 park computer N 2004-12-12 77.00 77.00 77.00 2.70 2.70 2.70 8.10 0
20220014 line computer N 2004-11-11 87.00 77.00 88.00 3.70 2.70 3.80 10.20 0
20220015 andy computer N 2004-10-10 78.00 46.00 89.00 2.80 0.00 3.90 6.70 0
20220016 joker computer N 2004-08-09 90.00 90.00 90.00 4.00 4.00 4.00 12.00 0
20220017 king computer N 2003-10-12 100.00 100.00 100.00 5.00 5.00 5.00 15.00 0
20220018 yeal computer N 2003-09-05 77.00 66.00 88.00 2.70 1.60 3.80 8.10 0
20220019 norm computer N 2003-02-02 34.00 66.00 56.00 0.00 1.60 0.00 1.60 0
俩个文件格式都为utf8