我是一名相对较新的程序员,目前正在大学学习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);
}
答案 0 :(得分:0)
如果找不到图书,则表示您正在致电main
。但是用户不应该调用main
。它由系统调用,是程序的开始。也许你的意思是return
?
您正在阅读BookList
,如果读取的记录不是您想要的记录,则将其写入BookList2
。找到记录后,编辑它然后将其写入BookList2
,然后关闭文件并将新文件重命名为旧文件。但旧文件中的所有剩余记录会发生什么?你不应该将它们复制到新文件中吗?
另请参阅有关其他有用消息的帖子的评论。