调试断言失败C - 不删除

时间:2016-05-30 20:03:28

标签: 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=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);


 }

1 个答案:

答案 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);
    }


 }