0

我正在尝试构建一个包含姓名、5 个成绩和平均水平的学生结构。然后我想扫描 5 名学生并将他们的信息写入二进制文件,然后我想更新 1 名学生的成绩......

我猜我调用函数更新的方式或我打开文件的方式是错误的......

希望你能帮我..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
#define G 5
#define S 1
typedef struct {
    char name[N];
    int grade[G];
    double average;
}student;

double aver(int a[G]);
void scan_student(student *student);
void print_student(student *student);
void update_grades(FILE *info);


void scan_student(student *student)
{
    int j;
flushall();
   printf("Enter the student's name\n");
   gets(student->name);
     for(j=0;j<G;j++)
        {
          printf("Enter the student's grade number --> %d\n",j);
          scanf("%d",&student->grade[j]);
        }
    student->average=aver(student->grade);

}

void print_student(student *STUD)

{
    int j;
    printf("this is the details of the student: \n");
    puts(STUD->name);
    for (j=0;j<G;j++)
    {
        printf("grade number --> %d\n",j);
        printf("%d\n",STUD->grade[j]);
    }
    printf("this is %s's average:\t\n%lf\n",STUD->name,STUD->average);
}










double aver(int a[G])
{
    double AVER;
    int sum=0,i;
    for (i=0;i<G;i++)
        sum+=a[i];
    AVER=sum/G;
    return AVER;
}


void update_grades()
{
    int size,i,boolean=0;
    char st[N];
    FILE* info;
    student student;
    size=sizeof(student);
    info=fopen("My Grades.txt","rb");
    printf("Enter the name of the student that you want to change he's details...\n");
    flushall();
    gets(st);
    rewind(info);
    do{

    fseek(info,size,SEEK_CUR);
fread(&student,size,1,info);
if(strcmp(student.name,st))
{
    boolean=1;
    printf("this is the selected student\n");

    print_student(&student);

    printf("\n\n wich gread would you like to change? choose from 1 to 5 :\n");
    scanf("%d",i);
    printf("insert new grade:\n");
    scanf("%d",student.grade[i-1]);

    printf("this is the student details after the changes:\n\n");
    print_student(&student);
    fseek(info,(-1)*size,SEEK_CUR);
fwrite(&student, size,1,info);
}
    }while((!feof(info))&&(!boolean));
if(!boolean)
    printf("\n\n there is no such student named %s....\n",st);
fclose(info);
}

void main()
{
int i,j,size;
char a;
student STUD;
FILE *txt;
txt=fopen("My Grades.txt","wb");
do{
size=sizeof(STUD);
scan_student(&STUD);
print_student(&STUD);
fwrite( &STUD,size,1,txt);
printf("would you like to add another student? Y/N\n");
    do{
        flushall();
    a=getchar();
    }while ((a!='Y')&&(a!='N'));
}while(a=='Y');

fclose(txt);
update_grades();
}

这是我得到的错误...... https://www.dropbox.com/s/pc9yz3a1w1cbcm1/example%20error.jpg

好的,伙计们..所以这是固定代码..但仍然是同样的问题..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
#define G 5
#define S 1
typedef struct {
    char name[N];
    int grade[G];
    double average;
}student;


double aver(int a[G]);
void scan_student(student *student);
void print_student(student *student);
void update_grades(FILE *info);


void scan_student(student *st)
{
    int j;
flushall();
   printf("Enter the student's name\n");
   gets(st->name);
     for(j=0;j<G;j++)
        {
          printf("Enter the student's grade number --> %d\n",j);
          scanf("%d",&st->grade[j]);
        }
    st->average=aver(st->grade);

}

void print_student(student *STUD)

{
    int j;
    printf("this is the details of the student: \n");
    puts(STUD->name);
    for (j=0;j<G;j++)
    {
        printf("grade number --> %d\n",j);
        printf("%d\n",STUD->grade[j]);
    }
    printf("this is %s's average:\t\n%lf\n",STUD->name,STUD->average);
}










double aver(int a[G])
{
    double AVER,sum=0;
    int i;
    for (i=0;i<G;i++)
        sum+=a[i];
    AVER=sum/G;
    return AVER;
}


void update_grades()
{
    int size,i,boolean=0;
    char st[N];
    FILE* info;
    student STUDENT;
    size=sizeof(student);
    info=fopen("My Grades.txt","rb");
    printf("Enter the name of the student that you want to change he's details...\n");
    flushall();
    gets(st);
    rewind(info);
    do{

    fseek(info,size,SEEK_CUR);
fread(&STUDENT,size,1,info);
if(strcmp(STUDENT.name,st))
{
    boolean=1;
    printf("this is the selected student\n");

    print_student(&STUDENT);

    printf("\n\n wich gread would you like to change? choose from 1 to 5 :\n");
    scanf("%d",&i);
    printf("insert new grade:\n");
    scanf("%d",&STUDENT.grade[i-1]);

    printf("this is the student details after the changes:\n\n");
    print_student(&STUDENT);
    fseek(info,(-1)*size,SEEK_CUR);
fwrite(&STUDENT, size,1,info);
}
    }while((!feof(info))&&(!boolean));
if(!boolean)
    printf("\n\n there is no such student named %s....\n",st);
fclose(info);
}


void main()
{
int i,j,size;
char a;
student STUD;
FILE *txt;
txt=fopen("My Grades.txt","wb");
do{
size=sizeof(STUD);
scan_student(&STUD);
print_student(&STUD);
fwrite( &STUD,size,1,txt);
printf("would you like to add another student? Y/N\n");
    do{
        flushall();
    a=getchar();
    }while ((a!='Y')&&(a!='N'));
}while(a=='Y');

fclose(txt);
update_grades();


}

好的,所以当我只添加 1 个学生时,我会在我共享的链接中收到此错误。如果我扫描了多个学生 ..并从文件中选择一个学生(不是第一个学生)...然后它会更新名单上在他之前的那个学生……

4

1 回答 1

0

错误可能是这样声明的:void scan_student(student *student);。也可以void scan_student(student *STUD);改变:

void scan_student(student *student)
{
...
}

作为

void scan_student(student *STUD)
{...}
于 2013-08-30T15:41:28.740 回答