这是我的程序,你可以忽略主要部分:
#include<stdio.h>
#include<string.h>
typedef struct{
char firstname[20];
char lastname[20];
int assignments[5];
int labs[5];
int midterm;
int finalM;
}myclass;
myclass gng1105[10];
void update();
void printList();
void addStudent();
int main(){
int option;
update();
while(option!= 3){
printf("Please enter the option number:\n1.View the information for a specific student\n2.add a student number\n3.Exist\n");
scanf("%d", &option);
if(option == 1){
printList();
}
else if (option == 2){
addStudent();
}
else if (option == 3){
printf("system closed");
break;
}
else{
printf("invalid input");
}
printf("******************************************************************************\n");
}
return 0;
}
void update(){
FILE *fp;
fp = fopen("myclass.txt", "r");
int i = 0;
if (fp == NULL)
printf("Error opening file\n");
else{
while(!feof(fp)){
fscanf(fp,"%s%s%d%d%d%d%d%d%d%d%d%d%d%d", gng1105[i].firstname,
gng1105[i].lastname,
&gng1105[i].assignments[0],
&gng1105[i].assignments[1],
&gng1105[i].assignments[2],
&gng1105[i].assignments[3],
&gng1105[i].assignments[4],
&gng1105[i].labs[0],
&gng1105[i].labs[1],
&gng1105[i].labs[2],
&gng1105[i].labs[3],
&gng1105[i].labs[4],
&gng1105[i].midterm,
&gng1105[i].finalM);
i++;
}
}
fclose(fp);
}
void printList(){
int i;
for(i = 0;i <10 ; i++){
printf("%s %s %d %d %d %d %d %d %d %d %d %d %d %d", gng1105[i].firstname,
gng1105[i].lastname,
gng1105[i].assignments[0],
gng1105[i].assignments[1],
gng1105[i].assignments[2],
gng1105[i].assignments[3],
gng1105[i].assignments[4],
gng1105[i].labs[0],
gng1105[i].labs[1],
gng1105[i].labs[2],
gng1105[i].labs[3],
gng1105[i].labs[4],
gng1105[i].midterm,
gng1105[i].finalM);
}
}
void addStudent(){
}
以下是txt文件的样子:
Marcus Hill 100001 64 54 68 71 60 55 43 30 70 0 70 81
Brian Wyllie 100002 90 96 89 91 93 90 85 89 95 100 100 90
Soren Bjerg 100003 70 84 74 75 90 81 70 0 77 82 73 80
Jason Tran 100004 53 0 56 50 0 69 30 50 51 67 70 65
Alex Chu 100004 85 85 83 80 65 94 89 79 89 69 50 90
这是输出,它看起来很奇怪,即使错误看起来不一致(也不能发布图片抱歉):
Marcus Hill 100001 64 54 68 71 60 55 43 30 70 0 7081 brian 0 0 0 0 0 0 0 0 0 0 0 0Willie 10002 90 96 89 91 93 90 85 95 100 100 90soren Bjerg 1000003 70 84 74 75 90 81 70 0 77 82 7380 0 0 0 0 0 0 0 0 0 0 0 0
(我强调错误,除了那些该死的零)
如果有人能帮助我,我真的很感激!
答案 0 :(得分:3)
您似乎忘记了studentId
中的struct
字段。您将学生ID号(例如100001)分配给gng1105[i].assignments[0]
。
您的struct
应该像这样定义:
typedef struct
{
char firstname[20];
char lastname[20];
int studentId; // <<<------ This is missing
int assignments[5];
int labs[5];
int midterm;
int finalM;
} myclass;
您需要一个额外的格式说明符和变量:
fscanf(fp,"%s%s%d%d%d%d%d%d%d%d%d%d%d%d%", gng1105[i].firstname,
gng1105[i].lastname,
&gng1105[i].studentId, // <<<--- This is missing
&gng1105[i].assignments[0],
&gng1105[i].assignments[1],
&gng1105[i].assignments[2],
&gng1105[i].assignments[3],
&gng1105[i].assignments[4],
&gng1105[i].labs[0],
&gng1105[i].labs[1],
&gng1105[i].labs[2],
&gng1105[i].labs[3],
&gng1105[i].labs[4],
&gng1105[i].midterm,
&gng1105[i].finalM);
不要忘记这一点:
printf("%s %s %d %d %d %d %d %d %d %d %d %d %d %d %d", gng1105[i].firstname,
gng1105[i].lastname,
gng1105[I].studentId, // <<<--- Missing
gng1105[i].assignments[0],
gng1105[i].assignments[1],
gng1105[i].assignments[2],
gng1105[i].assignments[3],
gng1105[i].assignments[4],
gng1105[i].labs[0],
gng1105[i].labs[1],
gng1105[i].labs[2],
gng1105[i].labs[3],
gng1105[i].labs[4],
gng1105[i].midterm,
gng1105[i].finalM);
不要忘记fscanf
和printf
都需要额外的%d
。