我正在编写一个程序,允许用户添加一个有4个答案,正确答案,日期,作者和复杂程度的问题,该程序还具有阅读所有问题和删除问题的功能。当我选择选项添加一个问题并插入所有特征时,错误的消息框出现,它也不允许我删除或查看`。 的问题。我需要帮助。
为什么以下代码不允许我删除问题?
void edit()
{
char filename[2];
int y;
int q,ft,s,t, fr,d,a,l,tr,n,da;
FILE *f, *f1;
f=fopen("pff.txt","r");
if (f==NULL)
{
perror ("Error!");
}
fscanf(f,"%d",&y);
printf(" " );
gets(question.name);
n=sizeof(question.name);
printf("Name : ");
gets(question.name);
q=sizeof(question.name);
printf("Answer 1: ");
gets(question.first);
ft=sizeof(question.first);
printf("Answer 2: ");
gets(question.second);
s=sizeof(question.second);
printf("Answer 3: ");
gets(question.third);
t=sizeof(question.third);
printf("Answer 4: ");
gets(question.fourth);
fr=sizeof(question.fourth);
printf("Right answer (1-4): ");
scanf("%d",&question.tr);
printf(" ");
gets(question.date);
da=sizeof(question.date);
printf("Date: ");
gets(question.date);
d=sizeof(question.date);
printf(" Author: ");
gets(question.author);
t=sizeof(question.author);
printf("Level (0-2): ");
scanf("%d",&question.level);
fclose (f);
sprintf(filename, "%d.bin", y+1);
puts (filename); f=fopen(filename,"wb");
fwrite(&q,sizeof(int),1,f);
fwrite(question.name,sizeof(question.name),1,f);
fwrite(&ft,sizeof(int),1,f);
fwrite(question.first,sizeof(question.first),1,f);
fwrite(&s,sizeof(int),1,f);
fwrite(question.second,sizeof(question.second),1,f);
fwrite(&t,sizeof(int),1,f);
fwrite(question.third,sizeof(question.third),1,f);
fwrite(&fr,sizeof(int),1,f);
fwrite(question.fourth,sizeof(question.fourth),1,f);
fwrite (&question.tr, sizeof (int),1,f);
fwrite(&d,sizeof(int),1,f);
fwrite(question.date, sizeof(question.date),1,f);
fwrite(&a,sizeof(int),1,f);
fwrite(question.author,sizeof(question.author),1,f);
fwrite(question.level,sizeof(int),1,f);
fclose(f);
f=fopen("pff.txt","w");
fprintf(f,"%d",y+1);
fclose(f);
}
答案 0 :(得分:0)
filename
太短,无法存储文件名。如果int
为32位长,则长度应至少为16个字符。fwrite(question.level,sizeof(int),1,f);
似乎有误,因为使用question.level
说明符将scanf
的地址传递给%d
,我想它的类型是int
。您应该包含正确的标头并启用编译器警告。NULL
返回的fopen
传递给fscanf
。gets
,它具有不可避免的缓冲区溢出风险,并且在C99中已弃用并在C11中被删除。试试这个(此处未解决使用gets
的问题):
void edit()
{
char filename[32]; /* allocate enough buffer */
int y;
int q,ft,s,t, fr,d,a,l,tr,n,da;
FILE *f, *f1;
f=fopen("pff.txt","r");
if (f==NULL)
{
perror ("Error!");
return; /* avoid using NULL as file pointer */
}
fscanf(f,"%d",&y);
printf(" " );
gets(question.name);
n=sizeof(question.name);
printf("Name : ");
gets(question.name);
q=sizeof(question.name);
printf("Answer 1: ");
gets(question.first);
ft=sizeof(question.first);
printf("Answer 2: ");
gets(question.second);
s=sizeof(question.second);
printf("Answer 3: ");
gets(question.third);
t=sizeof(question.third);
printf("Answer 4: ");
gets(question.fourth);
fr=sizeof(question.fourth);
printf("Right answer (1-4): ");
scanf("%d",&question.tr); /* warning: don't place newline character after the number to be read here, or what is read to question.date may become not what is wanted */
printf(" ");
gets(question.date);
da=sizeof(question.date);
printf("Date: ");
gets(question.date);
d=sizeof(question.date);
printf(" Author: ");
gets(question.author);
t=sizeof(question.author);
printf("Level (0-2): ");
scanf("%d",&question.level);
fclose (f);
sprintf(filename, "%d.bin", y+1);
puts (filename);
f=fopen(filename,"wb");
if (f == NULL) return; /* add error check */
fwrite(&q,sizeof(int),1,f);
fwrite(question.name,sizeof(question.name),1,f);
fwrite(&ft,sizeof(int),1,f);
fwrite(question.first,sizeof(question.first),1,f);
fwrite(&s,sizeof(int),1,f);
fwrite(question.second,sizeof(question.second),1,f);
fwrite(&t,sizeof(int),1,f);
fwrite(question.third,sizeof(question.third),1,f);
fwrite(&fr,sizeof(int),1,f);
fwrite(question.fourth,sizeof(question.fourth),1,f);
fwrite (&question.tr, sizeof (int),1,f);
fwrite(&d,sizeof(int),1,f);
fwrite(question.date, sizeof(question.date),1,f);
fwrite(&a,sizeof(int),1,f);
fwrite(question.author,sizeof(question.author),1,f);
fwrite(&question.level,sizeof(int),1,f); /* add & before question.level */
fclose(f);
f=fopen("pff.txt","w");
if (f != NULL) { /* add error check */
fprintf(f,"%d",y+1);
fclose(f);
}
}