2016-03-17 128 views
0

我是一个相对较新的程序员,目前在大学学习C language。对于我的项目,我的任务是为学生设计一个图书馆系统,每当我尝试删除最近编辑过的同一本书时,我一直在体验debug assertion failed-错误。有人可以帮我解决这个问题吗? PS:我必须在几天内提交程序,所以我需要一些帮助! d:调试断言失败,无法编辑和删除记录

这里是我的编辑书代码:

void EditBookInformation() 
{ 
    int found = 0; 
    char user_input[5]; 
    system("cls"); 
    printf("You have selected Edit Book Information \n"); 
    printf("Please enter the Book ID to edit the book: \n"); 
    scanf_s(" %s", &user_input, 5); 
    fflush(stdin); 
    fopen_s(&BookList, "record.txt", "rb+"); 
    fopen_s(&BookList2, "newrecord.txt", "wb+"); 
    fopen_s(&Logsheet, "log.txt", "a+"); 
    rewind(BookList); 
    rewind(BookList2); 
    while (fread(&Books, sizeof(Books), 1, BookList) != NULL) 
    { 
     if (strcmp(user_input, Books.Book_ID) == 0) 
     { 
      found = 1; 
      printf("Book has been found! \n"); 
      printf("\nBook ID:%s \n", Books.Book_ID); 
      printf("Title:%s \n", Books.Title); 
      printf("Edition:%s \n", Books.Edition); 
      printf("Year of publication:%s \n", Books.Year_of_publication); 
      printf("Shelf location:%s \n", Books.Shelf_location); 
      printf("Price in RM:%s \n", Books.Price); 
      break; 
     } 
     else 
     { 
      fwrite(&Books, sizeof(Books), 1, BookList2); 
     } 
    } 

    if (!found) 
    { 
     printf("Book not found! \n"); 
     _fcloseall(); 
     system("pause"); 
     system("cls"); 
     main(); 
    } 

    char confirm; 
    printf("\nDo you want to edit this book?"); 
    scanf_s("%c", &confirm); 
    fflush(stdin); 


    if (confirm == 'y') 
    { 
     printf("New Title:"); 
     scanf_s("%[^\n]s", &Books.Title, 50); 
     while (getchar() != '\n'); 

     printf("New Edition:"); 
     scanf_s("%s", &Books.Edition, 6); 
     while (getchar() != '\n'); 

     NewYearInput: 
     printf("New Year of publication:"); 
     scanf_s("%s", &Books.Year_of_publication, 5); 
     while (getchar() != '\n'); 
     int length = strlen(Books.Year_of_publication); 
     int digit = 0; 
     if (length == 4) 
     { 
      for (; digit < length; digit++) 
      if (!isdigit(Books.Year_of_publication[digit])) 
       break; 
     } 
     if (digit != 4) 
     { 
      printf("Wrong input! Please enter 4 digits for year! \n"); 
      system("pause>nul"); 
      goto NewYearInput; 
     } 

     printf("New Shelf Location:"); 
     scanf_s("%s", &Books.Shelf_location, 5); 
     while (getchar() != '\n'); 

     printf("New Price in RM:"); 
     scanf_s("%s", &Books.Price, 5); 
     while (getchar() != '\n'); 

     fprintf(Logsheet, "Book edited: %s \n", Books.Title); 
     fseek(BookList, ftell(BookList) -sizeof(Books), SEEK_SET); 
     fwrite(&Books, 1, sizeof(Books), BookList); 
     fclose(BookList); 
     fclose(Logsheet); 
     printf("Book has been edited! \n"); 
     printf("Press any key to return to main menu \n"); 
     system("pause>nul"); 
     system("cls"); 
     main(); 
    } 
    else if (confirm == 'n') 
    { 
     printf("You have cancelled your operation! \n"); 
     _fcloseall(); 
     system("cls"); 
     main(); 
    } 
} 

这里是我删除的书籍代码:

void DeleteBookByBookID() 
{ 
    int found = 0; 
    char user_input1[5]; 
    system("cls"); 
    printf("You have selected Delete Book \n"); 
    printf("Please enter the Book ID that you want to delete the book \n"); 
    scanf_s("%s", &user_input1, 5); 
    fflush(stdin); 
    fopen_s(&BookList, "record.txt", "rb+"); 
    fopen_s(&BookList2, "newrecord.txt", "wb+"); 
    fopen_s(&Logsheet, "log.txt", "a+"); 

    rewind(BookList); 
    rewind(BookList2); 

    while (fread(&Books, sizeof(Books), 1, BookList) != NULL) 
    { 
     if (strcmp(user_input1, Books.Book_ID) == 0) 
     { 
      printf("Book found! \n"); 
      found = 1; 
     } 
     else 
     { 
      fwrite(&Books, sizeof(Books), 1, BookList2); 
     } 
    } 
    if (!found) 
    { 
     printf("Book ID not found! \n"); 
     _fcloseall(); 
     system("pause"); 
     system("cls"); 
     main(); 
    } 
    fclose(BookList); 
    fclose(BookList2); 

    char confirm; 
    printf("Do you want to delete this book? \n"); 
    scanf_s(" %c", &confirm); 
    fflush(stdin); 

    if (confirm == 'y') 
    { 
     remove("record.txt"); 
     rename("newrecord.txt", "record.txt"); 
     fprintf(Logsheet, "Book deleted: %s \n", Books.Title); 
     printf("Book has been deleted! \n"); 
     printf("Press any key to return to main menu \n"); 
     system("pause>nul"); 
     system("cls"); 
     main(); 
    } 
    else if (confirm == 'n') 
    { 
     printf("You have cancelled your operation! \n"); 
     fclose(BookList); 
     fclose(BookList2); 
     system("cls"); 
     main(); 
    } 
    fclose(Logsheet); 
} 
+1

您正在使用哪个IDE? (如codeblocks,netbeans等) –

+1

那么,你是否在调试器中的代码? – OldProgrammer

+0

我正在使用Visual Studio 2013,是的,当我调试它出来。 – ocsquare

回答

0

如果没有找到一本书,你在呼唤main。但main不应该由用户调用。它被系统调用并且是程序的开始。也许你的意思是return

您正在阅读的是BookList,如果读取的记录不是您想要的,请将其写入BookList2。找到记录后,编辑它,然后将其写入BookList2,然后关闭文件并将新文件重命名为旧文件。但是旧文件中的所有剩余记录会发生什么?你不应该将这些文件复制到新文件中吗?

另请参阅您的帖子对其他有帮助的提示的评论。

+0

实际上,我的main()是我的主菜单,switch语句用于选择这样的选项,例如 – ocsquare

+0

为了防止任何混淆,链接错误或其他错误,切勿调用函数main。 –

+0

啊,我明白了,明天我会试试 – ocsquare