0

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!

Hi its Me
  • 1
  • 3

1 Answers1

0

You can replace the number in the file only if it occupies exactly the same number of bytes. But if that is the case, then your strategy could be:

  1. ftell the current position

  2. read the next record

  3. compare it with the record you want.

  4. if it is the right one, then fseek to the position you got from ftell

  5. write the record.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • Hey @Paul i was trying to do what you said but it didn't work. Maybe i'm doing it wrong,high possibility, but when i try it, the program keep finding the last record and doesn't replace it but append another one with the same data like the one i wanted to change(the integer i wanted was replaced). The student file was opened in append mode. Do u think thus might be the problem? Thanks again – Hi its Me Aug 09 '20 at 09:47
  • Your opening of `"rb+"` is correct. It means "open for reading and writing without CR/LF translation". But you must have an intermittent `fseek` before writing, otherwise it will write at the end. Carefully read the documentation of fseek, fwrite, and fopen! – Paul Ogilvie Aug 09 '20 at 09:49
  • @Peter thanks, I just realized how i was using wrong fseek and now works fine. – Hi its Me Aug 09 '20 at 11:28