C 调试断言失败-未删除
我正在编写一个程序,允许用户添加一个有4个答案的问题,正确答案,日期,作者和复杂程度,该程序还具有阅读所有问题和删除问题的功能。当我选择“添加问题”选项并插入所有特征时,出现错误的消息框,它也不允许我删除或查看“`”。 问题。我需要帮助 为什么下面的代码不允许我删除问题C 调试断言失败-未删除,c,C,我正在编写一个程序,允许用户添加一个有4个答案的问题,正确答案,日期,作者和复杂程度,该程序还具有阅读所有问题和删除问题的功能。当我选择“添加问题”选项并插入所有特征时,出现错误的消息框,它也不允许我删除或查看“`”。 问题。我需要帮助 为什么下面的代码不允许我删除问题 void edit() { char filename[2]; int y; int q,ft,s,t, fr,d,a,l,tr,n,da; FILE *f, *f1; f=fop
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);
}
- 缓冲区
太短,无法存储文件名。如果filename
的长度为32位,则长度应至少为16个字符int
- 在不知道实际定义的情况下,写入行
似乎是错误的,因为fwrite(question.level,sizeof(int),1,f)
的地址使用问题.level
说明符传递给%d
,我猜它的类型是scanf
。您应该包含正确的头并启用编译器警告int
- 您不应将可能从
返回的fopen
传递到NULL
fscanf
- 您不应该使用标准库中的
,因为它有不可避免的缓冲区溢出风险,在C99中被弃用,在C11中被删除get
的问题):
你忘了问一个问题。你需要什么帮助?帮助我认识到什么是我的错误,为什么它会失败?它以什么方式失败?你说了一些关于消息框的事情,但是消息框说什么呢?你说你不能做事,但不要告诉我们当你尝试时会发生什么。此外,没有任何东西表明您显示的代码应该做什么,因此很难判断它做错了什么。文件格式应该是什么?行?消息框显示调试断言失败表达式:(buffer!=NULL)消息框是否有debug
按钮?如果没有,您可以在您的平台上安装调试器吗?我注意到您在检查代码时没有错误——例如,您没有检查fopen
的返回值以查看它是否成功。加上这一点会有很大帮助。
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);
}
}