功能不是从文件中删除书籍,它应该删除所提供书籍的isbn号码,如果我们再次输入该isbn号码,如果已删除的书籍,则应显示“未找到记录”的消息,而是再次显示显示书的信息,即文件未删除
void deletingbooks() //displaying only deleting books function
{
system("cls");
int d;
char findbook;
char another = 'y';
while (another == 'y') //infinite loop
{
system("cls");
printf("Enter the Book ID to delete:");
scanf("%d", & d);
fp = fopen("library.dat", "rb+");
rewind(fp);
while (fread( & info, sizeof(info), 1, fp) == 1) {
if (info.isbn == d) {
printf("The book record is available\n\n\n\n");
printf("Book name is %s\n\n", info.name);
printf("Author name is %c\n\n", info.Author);
findbook = 't';
}
}
if (findbook != 't') {
printf("No record is found modify the search\n\n");
if (getch())
mainmenu();
}
if (findbook == 't') {
printf("Do you want to delete it?(Y/N):\n");
if (getch() == 'y') {
fd = fopen("test.dat", "wb+"); //temporary file for delete
rewind(fp);
while (fread( & info, sizeof(info), 1, fp) == 1) {
if (info.isbn != d) {
fseek(fd, 0, SEEK_CUR);
fwrite( & info, sizeof(info), 1, fd); //write all in tempory file except that
} //we want to delete
}
fclose(fd);
fclose(fp);
remove("library.dat");
rename("test.dat", "library.dat"); //copy all item from temporary file to fp except that
fp = fopen("library.dat", "rb+"); //we want to delete
if (findbook == 't') {
printf("The record is sucessfully deleted\n");
printf("\nDelete another record?(Y/N)");
}
} else
mainmenu();
fflush(stdin);
another = getch();
}
}
mainmenu();
}