c - 调试断言失败(文件名!= nullptr)

标签 c visual-studio-2015

我是初学者,正在尝试图书馆管理系统,结果出现错误(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);
}

Debug Assertion Failed!

Program: ...ments\Visual Studio 2015\Projects\Project9\Debug\Project9.exe File: minkernel\crts\ucrt\src\appcrt\stdio\fopen.cpp Line: 30

Expression: file_name != nullptr

For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

最佳答案

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

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

关于c - 调试断言失败(文件名!= nullptr),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35810766/

相关文章:

c# - 使用 .Netcore 库的 FxCop 分析在 VS2015 更新 3 中失败

使用循环 shell 命令行调用 popen() 管道流

c++ - 如何将宏扩展的结果分成不同的参数?

c++ - 如何从 CreateWindowEx() 窗口获取宽度和高度? C++

c - 返回结构数组或结构指针数组?

c - 如何在 C 函数中使用二维数组作为输出参数

c# - 如何使用非文本信息扩充 Visual Studio C# 编辑器?

c# - 如何在 Visual Studio 2015 中禁用实时编译

node.js - npm 安装在 Windows 10 上失败(node-gyp 重建)

C++ 错误 C3646、C2059 和 C2238 Visual Studio 2015(社区)