图书管理程序删除功能有问题

麻烦各位看看这个程序,补一下book.dat的数据https://wwlm.lanzouy.com/idwJD0w7ysgd


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
 
 
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKSS 21 // 多出来的一位为判断是否为第一次打开文件
#define MAXBKS 20  // 最大图书数量
char *s_gets(char *st, int n);
struct book
{ // 建立book模板
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
    int delete_flag; // 标记0为未删除 1为已删除
}; // 添加删除标记成员
int main(void)
{
    int count = 0;
    int add = 0;
    char bookname_deleta[MAXTITL];
    char choice;
    char space = ' ';
    char input[MAXTITL];
    FILE *fr;
    FILE *fw;
    fr = fopen("book.dat", "a+b");
    if (fr == NULL)
    {
        printf("Failed to open file!\n");
        exit(EXIT_FAILURE);
    }
    struct book arr[MAXBKSS];
    rewind(fr);
    while (count < MAXBKS && fread(&arr[count], sizeof(struct book), 1, fr) == 1)
    {
        if (count == 0)
        {
            printf("书籍信息如下:\n");
            printf("书名           作者             价格\n");
        }
        if (arr[count].delete_flag == 0)
        {
            printf("%-16s%-16s%.2f %d\n", arr[count].title, arr[count].author, arr[count].value, arr[count].delete_flag);
            count++;
        }
    }
    fclose(fr);
    if (count == MAXBKS)
    {
        printf("书籍已经放满了.");
        exit(2);
    }
    if ((arr[MAXBKSS - 1].delete_flag) != 1)
    {
        for (int i = 0; i < MAXBKSS; i++)
        {
            arr[i].delete_flag = 1;
        }
    } // 判断文件是否为首次打开
    struct book *ptr;
    ptr = &arr[0];
    while (1)
    {
        printf("请问需要删除文件吗?(请输入y或者n):");
        int result = scanf("%c", &choice);
        if (result == 1)
        {
            if (choice == 'y' || choice == 'Y')
            {
                printf("请输入想要删除的书籍:");
                scanf("%s", bookname_deleta);
                while (getchar() != '\n')
                    continue;
                for (int i = 0; i < count; i++)
                {
                    if (strcmp(arr[i].title, bookname_deleta) == 0)
                    {
                        arr[i].delete_flag = 1;
                    }
                }
            }
            else if (choice == 'n')
            {
                break;
            }
            else
            {
                printf("请重新输入,请输入y或者n(y代需要,n代表不需要):");
                result = scanf(" %c", &choice);
                while (getchar() != '\n')
                    continue;
            }
        }
    }
    while (getchar() != '\n')
        continue;
    printf("输入两个回车退出输入\n");
    printf("请输入书名:");
    count = 0;
    int books = 0;
    while (count < MAXBKS && s_gets((ptr + count)->title, MAXTITL) != NULL && (ptr + count)->delete_flag == 1)
    {
        if ((ptr + count)->title[0] != '\0')
        {
            printf("现在请输入作者名:");
            s_gets((ptr + count)->author, MAXAUTL);
            printf("现在请输入价格:");
            scanf("%f", &(ptr + count)->value);
            (ptr + count)->delete_flag = 0;
            while (getchar() != '\n')
                continue;
        }
        else if ((ptr + count)->title[0] == '\0')
        {
            break;
        }
        printf("请输入书名:\n");
        count++;
    }
    for (int i = 0; i < MAXBKS; i++)
    {
        if ((ptr + i)->delete_flag == 0)
        {
            printf("%d", (ptr + i)->delete_flag);
            books++;
        }
    }
    if (books == 0)
    {
        printf("文件中没有书");
        exit(1);
    }
    else if (books != 0)
    {
        for (int i = 0; i < books; i++)
        {
            if (arr[i].delete_flag == 0)
                printf("%-10s的作者是%-10s,价格为%.2f\n", arr[i].title, arr[i].author, arr[i].value);
        }
    }
    if ((fw = fopen("book.dat", "a+b")) == NULL)
    {
        printf("文件打开失败.");
        exit(EXIT_FAILURE);
    }
    if (fwrite(arr, sizeof(struct book), books, fw) != books)
    {
        printf("文件写入失败!\n");
        fclose(fw);
        exit(EXIT_FAILURE);
    }
    printf("%d", books);
    fclose(fw);
    printf("程序结束.");
    return 0;
}
char *s_gets(char *st, int n)
{
    char *ret_val;                 // 存储 fgets 函数的返回值
    char *find;                    // 存储查找到的换行符的位置
    ret_val = fgets(st, n, stdin); // 从标准输入读取字符串
    if (ret_val)                   // 如果读取成功
    {
        find = strchr(st, '\n');      // 查找换行符
        if (find)                     // 如果找到了换行符
            *find = '\0';             // 在此处放置一个空字符, 表示字符串的结束
        else                          // 如果没有找到换行符
            while (getchar() != '\n') // 丢弃多余的输入字符
                continue;
    }
    return ret_val; // 返回字符串的起始地址
}

我的运行环境是linux,在windows下运行可能会有问题,还需要各位自己改改,麻烦了,最好是在这个的基础上进行修改,不要改动太多,或者直接把别人做的程序复制一份发过来

晕,你的程序一上来就删除图书?book.dat的内容是什么。

提供一下book的实例数据,我这边帮你测试下

我试试

我看了一下你的程序,删除功能确实存在问题。具体来说,在删除图书时,你只是将该书籍的 delete_flag 标记设置为了1,但并没有对文件进行实际的删除操作。这就导致了在之后添加新的图书时,这些已经被标记为删除的图书依然会被读取出来并输出到文件中。也就是说,删除的操作并没有真正起到作用。

要解决这个问题,你可以通过重新创建一个新的文件,并将未被标记为删除的图书写入到这个新文件中,最后删除原文件并将新文件更名为原文件名。这样就实现了对文件的真正删除操作。

以下是修改后的代码,请参考:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>

#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 20  // 最大图书数量

char *s_gets(char *st, int n);
struct book
{ // 建立book模板
    char title[MAXTITL];
    char author[MAXAUTL];
    float value;
    int delete_flag; // 标记0为未删除 1为已删除
}; // 添加删除标记成员

int main(void)
{
    int count = 0;
    char bookname_delete[MAXTITL];
    char choice;
    FILE *fr, *fw;
    struct book arr[MAXBKS];

    fr = fopen("book.dat", "rb");
    if (fr == NULL)
    {
        printf("Failed to open file!\n");
        exit(EXIT_FAILURE);
    }

    while (count < MAXBKS && fread(&arr[count], sizeof(struct book), 1, fr) == 1)
    {
        if (count == 0)
        {
            printf("书籍信息如下:\n");
            printf("书名           作者             价格\n");
        }
        if (arr[count].delete_flag == 0)
        {
            printf("%-16s%-16s%.2f %d\n", arr[count].title, arr[count].author, arr[count].value, arr[count].delete_flag);
            count++;
        }
    }

    fclose(fr);

    if (count == MAXBKS)
    {
        printf("书籍已经放满了.");
        exit(2);
    }

    struct book *ptr = &arr[0];

    while (1)
    {
        printf("请问需要删除文件吗?(请输入y或者n):");
        int result = scanf(" %c", &choice); // 注意这里加上空格,以清除之前的缓存
        if (result == 1)
        {
            if (choice == 'y' || choice == 'Y')
            {
                printf("请输入想要删除的书籍:");
                scanf("%s", bookname_delete);
                while (getchar() != '\n')
                    continue;
                for (int i = 0; i < count; i++)
                {
                    if (strcmp(arr[i].title, bookname_delete) == 0)
                    {
                        arr[i].delete_flag = 1;
                    }
                }
            }
            else if (choice == 'n')
            {
                break;
            }
            else
            {
                printf("请重新输入,请输入y或者n(y代需要,n代表不需要):");
            }
        }
        while (getchar() != '\n')
            continue;
    }

    printf("输入两个回车退出输入\n");
    printf("请输入书名:");
    count = 0;
    int books = 0;

    while (count < MAXBKS && s_gets((ptr + count)->title, MAXTITL) != NULL)
    {
        if ((ptr + count)->title[0] != '\0')
        {
            printf("现在请输入作者名:");
            s_gets((ptr + count)->author, MAXAUTL);
            printf("现在请输入价格:");
            scanf("%f", &(ptr + count)->value);
            (ptr + count)->delete_flag = 0;
            while (getchar() != '\n')
                continue;
            count++;
        }
        else
        {
            break;
        }
        printf("请输入书名:\n");
    }

    for (int i = 0; i < MAXBKS; i++)
    {
        if ((ptr + i)->delete_flag == 0)
        {
            books++;
        }
    }

    if (books == 0)
    {
        printf("文件中没有书");
        exit(1);
    }
    else
    {
        for (int i = 0; i < books; i++)
        {
            if (arr[i].delete_flag == 0)
                printf("%-10s的作者是%-10s,价格为%.2f\n", arr[i].title, arr[i].author, arr[i].value);
        }
    }

    fw = fopen("book_new.dat", "wb");
    if (fw == NULL)
    {
        printf("Failed to open file!\n");
        exit(EXIT_FAILURE);
    }

    for (int i = 0; i < count; i++)
    {
        if (arr[i].delete_flag == 0)
        {
            fwrite(&arr[i], sizeof(struct book), 1, fw);
        }
    }

    fclose(fw);
    remove("book.dat");
    rename("book_new.dat", "book.dat");

    printf("程序结束.");
    return 0;
}

char *s_gets(char *st, int n)
{
    char *ret_val;                 // 存储 fgets 函数的返回值
    char *find;                    // 存储查找到的换行符的位置
    ret_val = fgets(st, n, stdin); // 从标准输入读取字符串
    if (ret_val)                   // 如果读取成功
    {
        find = strchr(st, '\n');      // 查找换行符
        if (find)                     // 如果找到了换行符
            *find = '\0';             // 在此处放置一个空字符, 表示字符串的结束
        else                          // 如果没有找到换行符
            while (getchar() != '\n') // 丢弃多余的输入字符
                continue;
    }
    return ret_val; // 返回字符串的起始地址
}

你可以测试看看

在删除书籍时,使用了字符串比较函数strcmp(),如果书名中包含空格,则无法正常删除;此外,确认是否需要删除时,使用了scanf()函数,这可能会导致程序出现预期之外的行为,可以考虑使用fgets()等安全的读取函数代替。

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
看了一下代码,发现删除图书的功能存在问题。

在用户输入想要删除的书籍名字之后,程序应该在已有的书籍列表中查找是否存在该书籍,如果存在,则将该书籍的删除标记设置为1,表示该书籍已删除。但是,在原程序中,并没有判断是否存在该书籍,直接将所有书籍的删除标记都设置为1。这显然是错误的。

我修改了一下代码,添加了判断书籍是否存在的功能,请参考:

while (1)
    {
        printf("请问需要删除文件吗?(请输入y或者n):");
        int result = scanf(" %c", &choice);
        if (result == 1)
        {
            if (choice == 'y' || choice == 'Y')
            {
                printf("请输入想要删除的书籍:");
                s_gets(bookname_deleta, MAXTITL); // 使用自定义的字符串读取函数
                for (int i = 0; i < count; i++)
                {
                    if (strcmp(arr[i].title, bookname_deleta) == 0 && arr[i].delete_flag == 0)
                    {
                        arr[i].delete_flag = 1;
                        printf("%s已删除\n", bookname_deleta);
                        break; // 如果找到该书籍并且删除成功,直接退出循环
                    }
                }
            }
            else if (choice == 'n')
            {
                break;
            }
            else
            {
                printf("请重新输入,请输入y或者n(y代需要,n代表不需要):");
                result = scanf(" %c", &choice);
                while (getchar() != '\n')
                    continue;
            }
        }
    }

代码修改后还可以改进的地方:

  1. 使用 gets 函数读取字符串会存在安全问题,建议使用自定义的字符串读取函数,比如本程序中的 s_gets 函数。

  2. 没有对用户输入的书籍名字进行过滤和检查,可能存在非法输入,建议添加一些检查和过滤,确保程序的稳定性和健壮性。

  3. 删除后,再次添加新书籍时,如果空间不足,则会直接退出程序,建议在程序中加入对删除后剩余空间的判断,如果剩余空间不足,则阻止用户继续添加书籍,或者给出提示,让用户删除一些书籍。

  4. 代码中有许多魔数,比如 MAXTITL、MAXBKSS 和 MAXBKS 等,建议改为使用 #define 或者 const 定义常量,以增加代码的可读性和可维护性。
    如果我的回答解决了您的问题,请采纳!