2016-03-05 117 views
0

我是一名初學者,我正在嘗試使用庫管理系統thingy,它出現錯誤出現(Debug Assertion Failed),並且表達式爲(file_name!= nullptr )。調試斷言失敗(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); 
} 

調試斷言失敗!

計劃:...發言:\的Visual Studio 2015年 \項目\ Project9 \調試\ Project9.exe文件: minkernel \ CRT顯示器\ ucrt的\ src \ appcrt \標準輸入輸出\ fopen.cpp行:30

表達式:file_name!= nullptr

有關程序如何導致斷言失敗的信息,請參閱有關斷言的Visual C++文檔。

(按重試來調試應用程序)

+0

不相關,但'fflush(stdin)'是未定義的行爲。 –

回答

1

的消息告訴你,你已經通過了NULL文件名fopen。實際上,librecord是一個未初始化的靜態變量,其初始值爲NULL。 (斷言是編程錯誤的測試;您不應該將NULL作爲文件名。)

使用合適的文件名初始化librecord或在程序執行期間分配文件名,然後再打開文件。 (打開文件後,檢查是否成功,你不能依賴你的dta基本文件實際存在和可讀)

0

斷言告訴你它裏面有什麼,例如你的代碼或代碼中的某處存在這個斷言。

assert(file_name != nullptr); 

基於您的代碼,它看起來像你調用一些用戶輸入後的功能,但你還沒有驗證或檢查輸入(危險)。 AddNewBook的聲明也看起來很奇怪即哪來的類型,即

int AddNewBook(librecord); 

會給我各種警告,因爲librecord未聲明,它是一個類型,宏還是什麼?您稍後在文件中聲明使用...

FILE *librecord; 

您需要顯示所有代碼或至少以某種方式將問題最小化。提供的代碼不會在我的機器上編譯,更不用說運行了。