首页 > 解决方案 > 如何在二进制模式下使用 C 中的嵌套结构

问题描述

我必须制作一个考试系统,进行考试,参加考试,将我从考试中获得的分数存储在学生数据中。我试图用嵌套结构来做。我存储了信息,但在考试和参加之后它我无法更新嵌套结构中的特定数据这是结构:

struct quest
{
    char question[MAX_LIMIT];
    char ans1[10],ans2[10],ans3[10],ans4[10];
    char answer[10];
    int count;
}q;

struct subject{
quest q[50];
}bio,math;

这是考试的部分。//不是有问题的部分

void create_exam()
{
    int ch,start_count=0;
    char n;
    FILE *f,*fp;
    

    printf("What subject do you want to create\n1.Biology \n2.Math\n");
    scanf("%d",&n);
    if(n==1)
    {
    f=fopen(name_bio,"wb");
    if(f==NULL)
    {
        printf("Error!\n");
        return;
    }
    printf("Exams have a standard with 6 questions!!");
    
    for(int i=1;i<=6;i++)
    {
        fflush(stdin);
        printf("\nGive the question number %d: ",i);
        scanf("%[^\n]%*c",bio.q[i].question);
        printf("\nGive the alternatives for the question %d: \n",i);
        fflush(stdin);
        scanf("%[^\n]%*c\n",bio.q[i].ans1);
        scanf("%[^\n]%*c\n",bio.q[i].ans2);
        scanf("%[^\n]%*c\n",bio.q[i].ans3);
        scanf("%[^\n]%*c",bio.q[i].ans4);
        printf("Give the correct answer: ");
        scanf("%[^\n]%*c",bio.q[i].answer);
        bio.q[i].count=start_count;
    }
        
size_t unitsWritten=fwrite(&bio,sizeof(struct subject),1,f);
    
    
    if(unitsWritten==0){
        perror("\n\n Data not saved"); }
        else printf("\nData saved!");

    fclose(f);
return;}
else if(n==2)
{// the same thing with the other subject

这是有问题的部分。参加考试的部分。问题是当我想更新问题的错误计数器时,它不再在屏幕上打印问题。只是说“问题编号 %d”的部分,而不是空格

void get_exam()
{
    int temp;
    int id_temp;
    int n,sum=0;
    char ans[20];
    FILE *finside;
    finside=fopen(file_person,"rb+");
    if(finside==NULL)
    {
        printf("Error in file");
        return;
    }
    printf("\nGive the  Id : ");
    scanf("%d",&id_temp);

nr_person=get_nr();//to get the number of students saved

bool found=false;   //The part to check if the student ID is found so he can make the exam
for(int i=0;i<nr_person;i++)
{   fseek(finside,i*sizeof(struct person),SEEK_SET);
   fread(&nr1[i],sizeof(struct person),1,finside);
    
    if(id_temp==nr1[i].id)
{
       found=true;
       temp=id_temp;}
}

if(found==false)
{
    perror("\nId not found!");
    return;
}

    printf("Choose the subject:\n");
    printf("1.Biology\n");
    printf("2.Math\n");
    scanf("%d",&n);
    
    switch (n)
    {
        case 1:
        FILE *f;
        f=fopen(name_bio,"rb+");
        if(f==NULL)
        {
         printf("Gabim ne hapjen e file");
            return; 
        }
        int nr;
    

        system("cls");
        printf("====Start=====\n");

    for(int k=1;k<=6;k++)
    {
        //fseek(f,sizeof(struct quest)*(k-1),SEEK_SET);
        fread(&bio,sizeof(struct subject),1,f);
        fflush(stdin);
        printf("\nQuestion nr %d\n",k);
        printf("%s\n",bio.q[k].question);
        printf("%s\n",bio.q[k].ans1);
        printf("%s\n",bio.q[k].ans2);
        printf("%s\n",bio.q[k].ans3);
        printf("%s\n",bio.q[k].ans4);
    
        printf("Give the correct answer:");
        scanf("%[^\n]%*c",ans);

        
        if(strcmp(info.q[k].answer,ans)==0)
        {
            sum++;
        }
        
        else if(strcmp(info.q[k].ans4,ans)==0)
        {
            sum=+0;
        }
        else 
        {
            sum--;    //This is the part i can't do right.How can i use the `fseek` and `fwrite` right 
            bio.q[k].count++;                                     //in this case
            fseek(f,(k-1)*sizeof(struct quest),SEEK_SET);
            fwrite(&bio.q[k].count,sizeof(struct quest),1,f);
        }
}

size_t unitsWritten=fwrite(&bio,sizeof(struct lenda),1,f);
    
        if(unitsWritten==0){
        perror("\n\n Data not saved"); }
        else printf("\nData saved!");    
    printf("\nThe exam is over!\n");
    printf("\nU got %d points",sum);

//The part to save the points to students
/*
for(int i=0;i<nr_person;i++)
{
******
}
fclose(f);//File for the bio exam
fclose(finside);//File for student data
break;

先感谢您!!

标签: cfilenestedstructure

解决方案


推荐阅读