已经有一个学生信息文本想根据他们的总绩点计算排名,但是出错了
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
我这里测试,除了打印成绩那里需要改进下,其他地方都没太大问题。
需要注意的一个地方是,这个数据文件“学生信息.txt”一般是需要和代码放置在同一目录。
测试如下:
参考链接:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct student_information{
char id[20];
char name[20];
char major[20];
char graduate;
char birthday[20];
float gaoshu;
float lisan;
float luoji;
float gpa_gaoshu;
float gpa_lisan;
float gpa_luoji;
float gpa;
int rank;
};
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");
// https://blog.csdn.net/m0_65601072/article/details/124651590
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名
}
// printf("i=%d,temp=%d\n",i,temp);
st[i].rank=temp;
}
//更新记录
fp=fopen("全部排名后学生信息.txt","w");
for(i=0;i<n;i++)
{
for(int j=0;j<n;j++){
// 这里打印成绩修改下,按照当前学生结构信息里rank来进行排序打印
if(st[j].rank==(i+1)){
fprintf(fp,"%s %s %s %c %s %.2f %.2f %.2f %.2f %.2f %.2f %.2f %d\n",st[j].id,st[j].name,st[j].major,st[j].graduate,
st[j].birthday,st[j].gaoshu,st[j].lisan,st[j].luoji,st[j].gpa_gaoshu,
st[j].gpa_lisan,st[j].gpa_luoji,st[j].gpa,st[j].rank);
}
}
}
fclose(fp);
printf("数据重新计算成功\n");
getch();
}
int main(void){
all_caculate_rank(19);
return 0;
}
学生信息.txt(测试数据文件,和代码放置在同一个目录):
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
修改如下,改动处见注释,供参考:
#include <stdio.h>
#include <string.h>
struct student_information {
char id[10];
char name[16];
char major[32];
char graduate;
char birthday[11];
float gaoshu, lisan, luoji, gpa_gaoshu, gpa_lisan, gpa_luoji, gpa;
int rank;
};
void all_caculate_rank(int n) {
struct student_information st[100];
int i, j, temp, count = 0;
FILE* fp;
fp = fopen("D:\\学生信息.txt", "r");
if (fp == NULL)
{
printf("打开文件失败!\n");
return; // 修改
//getch();
//exit(0);
}
fseek(fp, 0L, SEEK_SET);
for (i = 0; i < n; i++) {
count = fscanf(fp, "%s %s %s %c %s %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");
break; // 修改
//printf("%d", count);
//getch();
//exit(0);
}
}
fclose(fp);
//计算排名并存储
for (n = i, 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("D:\\全部排名后学生信息.txt", "w");
for (i = 0; i < n; i++)
{
fprintf(fp, "%s %s %s %c %s %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();
}
int main()
{
all_caculate_rank(100);
return 0;
}
运行效果如下:
fscanf遇到空格和换行时结束,注意空格时也结束