名前、5 成績、および平均を含む学生の構造体を構築しようとしています。次に、5 人の学生をスキャンし、その情報をバイナリ ファイルに書き込み、その後、学生の 1 人の成績を更新したいと考えています...
関数 update を呼び出す方法またはファイルを開く方法が間違っていると思います...
あなたが私を助けてくれることを願っています..
#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人だけ追加すると、共有したリンクのようにこのエラーが発生します..そして、複数の生徒をスキャンすると..ファイルから生徒を選択します(最初の生徒ではありません)...その後、更新されますリストの彼の前にいた学生...