I'm making a program for school where i have to create a function for making exams and another one to get the exam. I did the part to create the exam. Now I'm trying to make the part to get the exam. The problem i have in this part is to store the points that are taken from the exam in the student data. I was trying to use "fseek()" but i can't do it right. Here is what i have tried:
void take_exam()
{
int id_temp;
int n,sum=0;
char answer[20];
printf("Choose the subject:\n");
printf("1.Math\n");
printf("2.English\n");
printf("3.Physics\n");
scanf("%d",&n);
FILE *finside;
finside=fopen(name_student,"rb+"); //open the student file. The students are registered in ab+ mode
if(finside==NULL)
{
printf("Error in file");
return;
}
switch (n)//used switch case for the different subjects
{
case 1:
FILE *f;
f=fopen(name_math,"rb");//opened the math file, where i have stored the question and answers
if(f==NULL)
{
printf("Error in file");
return;
}
while(fread(&math,sizeof(struct subject),1,f))
{
system("cls");
printf("Enter your ID");
scanf("%d",&id_temp);
printf("====Start the exam=====\n");
for(int i=1;i<=6;i++)// I have done each exam with 6 questions
{
fflush(stdin);
printf("\nQuestion number %d\n",i);
printf("%s\n",math.q[i].question);
printf("%s\n",math.q[i].ans1);
printf("%s\n",math.q[i].ans2);
printf("%s\n",math.q[i].ans3);
printf("%s\n",math.q[i].ans4);
printf("Give your answer:");
scanf("%[^\n]%*c",answer);
// The part of points calculate fine
if(strcmp(math.q[i].answer,answer)==0)
{
sum++;
}
else if(strcmp(math.q[i].ans4,answer)==0)
{
sum=+0;
}
else if((strcmp(math.q[i].answer,answer)!=0) && (strcmp(math.q[i].ans4,answer)!=0))
{
sum--;
}
math.q[i].points=sum;
}
printf("The exam is over!");}
printf("You got %d points",sum);
while(fread(&s[nr_student],sizeof(struct stud),1,finside))
{
fseek(finside,sizeof(struct stud),SEEK_SET);//The points are stored in this part but that student
if(s[nr_student].ID==id_temp) // is repeated again. One with the primary points
{ // and the other one with the points he's got.
s[nr_student].points+=sum;
}
fwrite(&s[nr_student],sizeof(struct stud),1,finside);
}
fclose(finside);
fclose(f);
break;
case 2:
*** //and so on for the other subjects
The output i get: ID: 1 Name:John Points:0
ID:1 Name:John Points:6
ID:2 Name:Bill Points:0
Thanks in advance!