首页 > 解决方案 > 我不能用c语言从图书馆管理系统中删除书籍

问题描述

功能不是从文件中删除书籍,它应该删除所提供书籍的isbn编号,如果我们再次输入该isbn编号,如果书籍已被删除,它应该显示“找不到记录”的消息,而是再次显示信息书的ie文件没有被删除

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

标签: c

解决方案


推荐阅读