【问题标题】:Debug Assertion Failed (file_name != nullptr)调试断言失败 (file_name != nullptr)
【发布时间】:2016-03-05 06:09:30
【问题描述】:

我是初学者,我正在尝试使用图书馆管理系统,结果出现错误 (Debug Assertion Failed),表达式为 (file_name != nullptr)。

当我在主菜单中,当我选择第一个选项时,出现了这个错误,所以我需要帮助,谢谢:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define _CRT_SECURE_NO_WARNING

int AddNewBook(librecord);
int Exit();

struct
{
   int id;
   char title;
   char edition;
   int year;
   char location;
   char price;
   int status;
}book;

FILE *librecord;

char book_id;
char book_title;
char book_edition;
char book_year;
char book_location;
char book_price;

char confirmation;
int no_value;

int main(void)
{
   printf("   **         ***          ***     **********   \n");
   printf("   **         ****        ****     **********   \n");
   printf("   **         ** **      ** **     ***          \n");
   printf("   **         **  **    **  **     **********   \n");
   printf("   **         **   **  **   **            ***   \n");
   printf("   *******    **    ****    **     **********   \n");
   printf("   *******    **     **     **     **********   \n");
   printf("\n");
   printf(" Welcome to Library Management System \n");
   printf("\n");
   printf(" MAIN MENU \n");
   printf("\n");
   printf(" 1. Add New Book \n");
   printf(" 2. Edit Book Information \n");
   printf(" 3. Delete Book \n");
   printf(" 4. View Book List \n");
   printf(" 5. Book Check-In \n");
   printf(" 6. Book Check-Out \n");
   printf(" 7. Search \n");
   printf(" 8. Exit \n");

   int choice;
   printf("\n Please enter a number: ");
   scanf_s("%d", &choice);

   switch(choice)
   {
     case 1: 
        system("cls");
        AddNewBook(librecord);
        break;

     case 8: 
        Exit();
     default:
        printf("Wrong Input !!! Please re-enter a number!!! \n");
        system("pause");
        system("cls");
        main();
   }

}

int AddNewBook(FILE *librecord)
{
   librecord = fopen(librecord, "ab+");

   printf("\n");
   printf(" ADD NEW BOOK \n");
   printf("\n");

   printf(" Book ID: ");
   scanf_s(" %d", &book.id);
   fflush(stdin);
   strcpy(book.id, book_id);

   printf("\n Title: ");
   scanf_s(" %s", &book.title);
   fflush(stdin);
   strcpy(book.title, book_title);

   printf("\n Edition: ");
   scanf_s(" %s", &book.edition);
   fflush(stdin);
   strcpy(book.edition, book_edition);

   printf("\n Year of Publication: ");
   scanf_s(" %d", &book.year);
   fflush(stdin);
   strcpy(book.year, book_year);

   printf("\n Shelf Location: ");
   scanf_s(" %s", &book.location);
   fflush(stdin);
   strcpy(book.location, book_location);

   printf("\n Price(RM): ");
   scanf_s(" %s", &book.price);
   fflush(stdin);
   strcpy(book.price, book_price);

   printf("Confirm? (Y/N) \n");
   scanf("%c", &confirmation);
}

int Exit()
{
   exit(0);
}

调试断言失败!

程序:...ments\Visual Studio 2015\Projects\Project9\Debug\Project9.exe 文件: minkernel\crts\ucrt\src\appcrt\stdio\fopen.cpp 行:30

表达式:file_name != nullptr

有关您的程序如何导致断言失败的信息, 请参阅有关断言的 Visual C++ 文档。

(按重试调试应用程序)

【问题讨论】:

  • 不相关,但fflush(stdin) 是未定义的行为。

标签: c visual-studio-2015


【解决方案1】:

消息告诉您,您已将NULL 文件名传递给fopen。事实上,librecord 是一个未初始化的静态变量,其初始值为NULL。 (断言是对编程错误的测试;您不应该将NULL 作为文件名传递。)

在打开文件之前,在程序执行期间使用合适的文件名初始化librecord 或指定文件名。 (打开文件后,检查是否成功。您不能依赖实际存在且可读的 dta 基本文件。)

【讨论】:

    【解决方案2】:

    断言告诉你其中有什么,即在你的代码或你使用的代码中的某个地方存在这个断言。

    assert(file_name != nullptr);
    

    根据您的代码,您似乎在某些用户输入后调用了一个函数,但您尚未验证或检查输入(危险)。 AddNewBook 的声明看起来也很奇怪,ie 的类型在哪里

    int AddNewBook(librecord);
    

    会给我各种警告,因为 librecord 没有声明,它是一个类型,一个宏还是什么?您稍后在文件中声明它使用...

    FILE *librecord;
    

    您需要显示所有代码或至少以某种方式最小化问题。提供的代码不会在我的机器上编译,更不用说运行了。

    【讨论】:

      猜你喜欢
      • 2020-06-24
      • 2020-02-29
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多