我试图让程序读取一个文本文件,其中包含诸如“1001 姓名 姓氏 10 20 30”之类的信息、学生人数、姓名、姓氏和他的 3 个年级。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int number;
char name[30];
char surname[30];
int midterm1,midterm2,midterm3;
} Student;
int comp(const void * aa, const void * bb)
{
struct Student a = *(struct Student*)aa;
struct Student b = *(struct Student*)bb;
if (a.midterm1==b.midterm1)
return 0;
else if (a.midterm1 < b.midterm1)
return -1;
else
return 1;
} // comp
int main(void)
{
int choice,studentnumber,midterm1,midterm2,midterm3,i,n;
char surname;
FILE *cfPtr;
struct student *name;
name = malloc( 10 * sizeof(Student));
if ((cfPtr = fopen("grades.txt", "r")) == NULL)
return 1;
const int STUDENTSMAX = 100;
struct Student students[STUDENTSMAX];
char buff[1024];
while(1)
{
memset(buff, 0, sizeof(buff));
fgets(buff, sizeof(buff) -1, cfPtr);
if (feof(cfPtr)) {
break;
}
sscanf(buff, "%d %s %s %d %d %d", &students[i].number, students[i].name, students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
printf("%4d %15s %15s %10d %10d %10d\n", students[i].number, students[i].name, students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
i++;
} // while
while (!feof(cfPtr))
{
fscanf(cfPtr, "%d%s%s%d%d%d", &students[i].number, &students[i].name,&students[i].surname, &students[i].midterm1, &students[i].midterm2, &students[i].midterm3);
printf("%4d%15s%15s%10d%10d%10d\n", students[i].number, students[i].name,students[i].surname, students[i].midterm1, students[i].midterm2, students[i].midterm3);
i++;
} // while
printf("What would you like to do? \n"
"1- Sort according to midterm 1\n"
"2- Sort according to midterm 2\n"
"3- Sort according to midterm 3\n"
"4- Exit\n");
scanf("%d",&choice);
scanf("%d",&choice);
switch (choice) {
case 1:
qsort(students, i, sizeof(struct Student), comp);
for (n = 0; n < i; n++) {
printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm1);
} // for
break;
case 2:
qsort(students, i, sizeof(struct Student), comp);
for (n = 0; n < i; n++) {
printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm2);
} // for
break;
case 3:
qsort(students, i, sizeof(struct Student), comp);
for (n = 0; n < i; n++) {
printf("%4d %15s %15s %10d %10d %10d\n", students[n].number, students[n].name, students[n].surname, students[n].midterm3);
} // for
break;
} // switch
} // main?
fclose(cfPtr);
system("PAUSE");
return 0;
} // the editor failed to find the corresponding '{' !OP should fix this
此当前代码向我返回此错误:
95 C:\Users\UseR\Desktop\main.c [Warning] parameter names (without types) in function declaration
95 C:\Users\UseR\Desktop\main.c [Warning] data definition has no type or storage class
98 C:\Users\UseR\Desktop\main.c syntax error before string constant
98 C:\Users\UseR\Desktop\main.c [Warning] data definition has no type or storage class
C:\Users\UseR\Desktop\Makefile.win [Build Error] [main.o] Error 1
该程序正在编译和运行没有任何问题,但它仍然给出错误并且根本没有排序。我在这里做错了什么?
编辑:这是我的输入文件;
100201 al beaver 40 50 70
100202 andrew matthews 30 90 75
100203 leah doga 60 55 80
100204 rob kurt 45 80 60
100205 aliah devon 65 70 50
100206 sally pir 70 40 85
100207 eric bekta 75 65 55
100208 nile coul 55 75 65
100209 mina umur 72 60 90
100210 john hot 73 63 87
PS 是的,这是在任何人问之前的功课,我的时间不多了,而且我已经尝试了好几天,没有任何扎实的进展。我被问到的主要是“
- 按降序排列所选考试的成绩,输出文件将仅包含三列:所选考试的姓名、姓氏和分数。
- 定义一个名为“student”的结构体,将每个学生的姓名、姓名和分数存储在同一个变量名下。因此,文件中的所有数据都将存储在一个名为“学生”的新数据类型数组中。”