如何在二进制模式下使用C中的嵌套结构
我必须制作一个考试系统,包括考试,参加考试,将我从考试中获得的分数存储在学生数据中。我尝试使用嵌套结构。我存储了信息,但在进行考试和参加考试后,我无法更新嵌套结构中的特定数据 结构如下:如何在二进制模式下使用C中的嵌套结构,c,file,nested,structure,C,File,Nested,Structure,我必须制作一个考试系统,包括考试,参加考试,将我从考试中获得的分数存储在学生数据中。我尝试使用嵌套结构。我存储了信息,但在进行考试和参加考试后,我无法更新嵌套结构中的特定数据 结构如下: 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[
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
void create_考试()
{
int ch,开始计数=0;
字符n;
文件*f,*fp;
printf(“你想创建什么科目\n1.生物学\n2.数学\n”);
scanf(“%d”和“&n”);
如果(n==1)
{
f=fopen(名称“wb”);
如果(f==NULL)
{
printf(“错误!\n”);
返回;
}
printf(“考试有6个问题的标准!!”;
对于(int i=1;您需要做的第一件事是修复代码格式。确保大括号和空格都在正确的位置,并且始终使用一致的大括号样式。sum=+0;
是一个NoOp,您是否希望它成为sum++
“我不知道"因此,他没有失分,但分数是+。这是可以删除的。我有一个问题,那就是如何在不让问题消失的情况下存储问题的错误计数器。你有没有试过用调试器进行调试?你没有发布错误,所以我们不能只复制代码并在本地测试,而不对问题进行一些猜测正如@RobertHarvey所指出的,你确实需要专注于你的代码卫生。你的实际代码执行路径,甚至可能不是你所期望的。
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;