gpt4 book ai didi

c - 编辑和删除记录的调试断言失败

转载 作者:行者123 更新时间:2023-11-30 17:03:42 25 4
gpt4 key购买 nike

我是一名相对较新的程序员,目前正在大学学习C 语言。对于我的项目,我的任务是为学生设计一个图书馆系统,每当我尝试删除最近编辑过的同一本书时,我都会遇到调试断言失败错误。有人可以帮我解答这个问题吗?

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);
}

最佳答案

如果没有找到一本书,您将调用main。但是main 不应该由用户调用。它由系统调用,是程序的开始。也许你的意思是返回

您正在从 BookList 读取数据,如果读取的记录不是您想要的记录,则将其写入 BookList2。找到记录后,对其进行编辑,然后将其写入 BookList2,然后关闭文件并将新文件重命名为旧文件。但是旧文件中的所有剩余记录会发生什么情况呢?您不应该将它们也复制到新文件中吗?

另请参阅您帖子的评论以获取其他有用的建议。

关于c - 编辑和删除记录的调试断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36064176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com