c语言图书管理系统问题

请问怎么把这个管理系统输入的数据保存下来,
关闭程序再打开时仍能查询到上次录入的图书啊?

#include
#include<string>
#include
#include
#include
#include <list>
#include
#include                      //标准库函数 
#include                     //使用system语句要调用windows函数库 
#include                       //输入输出函数库 


using namespace std;

void color(int x)                       //改变输出字体颜色函数 
{
    if(x>+0&&x<+15)
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),x);
    else
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
}


class item
{
    public:
    string name;
    string item_type;

    bool Register;


};


//书籍类
class Book : public item
{
public:
    Book() { borrow_flag = false; }   //无参构造函数
    Book(string name, string num, string auther,string price,string number)
        :name(name), num(num), auther(auther) {
        borrow_flag = false;
    }  //有参构造函数
    void setReader(string reader, int lcn, string data); //设置读者
    void setInfo(string name, string num, string auther,string price,string number); //设置书籍信息
    string getName() {
        return name;
    }
    string getNum() { return num; }
    string getAuther() {
        return auther;
    }
    bool getBorrow_flag() {
        return borrow_flag;
    }
    string getReader() {
        return reader;
    }
    int getLcn() {
        return lcn;
    }
    string getData() {
        return data;
    }
    bool isBorrow() { return borrow_flag; }        //判断书籍是否借出
    void setBorrow_flag(bool b) { borrow_flag = b; }
    void showInfo();        //显示数据信息
private:
    string name;  //书名
    string num;   //编号(唯一标示)
    string auther; //作者
    string price;
    string number; 

    bool borrow_flag;
    string reader; //读者
    int lcn;       //借书证号
    string data;   //借书日期
};

//用户
class Person
{
public:
    string Name;
    string Adress;
    list Regist_items;
};



void Book::setReader(string reader, int lcn, string data)
{
    borrow_flag = true;
    this->reader.assign(reader);
    this->lcn = lcn;
    this->data.assign(data);
}
void Book::setInfo(string name, string num, string auther,string price,string number)
{
    this->name.assign(name);
    this->num.assign(num);
    this->auther.assign(auther);
    this->price.assign(price);
    this->number.assign(number);
}
void Book::showInfo()
{
    cout << "书籍名称:" << setiosflags(ios_base::left) << setw(56) << name << endl
         << "书籍编号:" << setw(56) << num<< endl
         << "书籍作者:" << setw(56) << auther  << endl;
    if (borrow_flag)
    {
       cout << "书籍被借出。                                                      \n"
            << "读者姓名:" << setw(56) << reader<< endl
            << "借书证号:" << setw(56) << lcn << endl
            << "借书日期:" << setw(56) << data << endl;
    }
    else {
        cout << "书籍未被借出。                                                    \n";
    }
}
class Library
{
public:
    //书籍库
    list itemList;
    Library() { currentNum = 0; brrowNum = 0; }
    void addBook();                //向图书馆里加书籍
    void addBook(string name, string num, string auther,string price,string number);
    void deleteBook();   //删除无用书籍
    void brrowBook();   //借书,之前先判断书籍是否存在
    void returnBook();   //还书
    void getReader();
    void checkbook();  //查询某编号的书是谁借了
    int indexOfNum(string num); //根据编号得到书在数组中的下标
    vector getBooks() {
        return books;
    }
    void showInfo();
    int getTotalBooks() { return currentNum + brrowNum; }
private:
    vector books;//所有书籍
    map<string, int> readers;  //存储读者及其所借的书籍数目 
    int currentNum;   //库存书籍数目(不包括借出的)
    int brrowNum;     //借出书籍数目
};

void Library::addBook()
{
     FILE *fp=NULL; 
     fp=fopen("book1.dat","ab+");   
    Book b;
    int temp;
    string name, num, author,price,number;
    cout << "  *****************************增加界面*****************************\n\n";
    do {
        cout << "输入书籍名称,编号,作者,单价,数量:";
        cin >> name >> num >> author >> price >>number;
        b.setInfo(name, num, author,price,number);
        if (indexOfNum(num) == -1) {
            books.push_back(b);
            currentNum++;
            cout << "\n添加成功。" << endl;
            cout << "输入1继续增加,返回上一层输入2:";
            cin >> temp;
        }
        else {
            cout << "已存在该编号的书籍,添加失败。" << endl;
            cout << "输入1继续重新增加,返回上一层输入2:";
            cin >> temp;
        }
    } while (temp == 1);
    system("pause");
    system("cls");
    fclose(fp);
}
void Library::addBook(string name, string num, string auther,string price,string number)
{
    FILE *fp=NULL; 
     fp=fopen("book1.dat","ab+");  
    Book b;
    b.setInfo(name, num, auther,price,number);
    books.push_back(b);
}
void Library::deleteBook()
{
    
    FILE *fp=NULL; 
    fp=fopen("book1.dat","ab+"); 
    fseek(fp, 0, SEEK_SET); 
    int index, temp;
    string num;
    cout << "  *****************************删除界面*****************************\n\n";
    do {
        cout << "输入要删除的书籍的编号:";
        cin >> num;
        index = indexOfNum(num);
        if (index != -1) {
            if (!books[index].getBorrow_flag()) {
                cout << "删除的书籍的信息:\n";
                books[index].showInfo();
                books.erase(books.begin() + index);
                currentNum--;
                cout << "删除成功。" << endl;
                cout << "输入1继续继续删除,返回上一层输入2:";
                cin >> temp;
            }
            else {
                cout << "删除失败!书籍已经被借出。" << endl;
                cout << "输入1继续继续删除,返回上一层输入2:";
                cin >> temp;
            }
        }
        else
        {
            cout << "删除失败。未找到编号为" << num << "的书籍。\n";
            cout << "输入1继续继续删除,返回上一层输入2:";
            cin >> temp;
        }
    } while (temp == 1);
    system("pause");
    system("cls");
    fclose(fp);
}
void Library::brrowBook()
{
    FILE *fp=NULL; 
     fp=fopen("book1.dat","ab+");  
    string num;
    int index;
    cout << "  *****************************借阅界面*****************************\n\n";
    cout << "输入要借阅的书籍的编号:";
    cin >> num;
    index = indexOfNum(num);
    if (index != -1) {
        if (books[index].isBorrow()) {
        printf("书籍被借出。");
        system("pause");
        system("cls");
        }
        else
        {
            cout << "要借的书籍的信息:\n";
            books[index].showInfo();
            string reader, data;
            int lcn;
            cout << "输入读者姓名,借书证号,借书日期:";
            cin >> reader >> lcn >> data;
            if (readers[reader] != 2) {
                books[index].setReader(reader, lcn, data);
                cout << "借书完成。\n";
                currentNum--;
                brrowNum++;
                readers[reader]++;
                system("pause");
                system("cls");
            }
            else
            {
                cout << "借书失败,该读者已借超过两本书籍。\n";
                system("pause");
                system("cls");
            }
        }
    }
    else
    {
        cout << "借书失败。未找到编号为" << num << "的书籍.\n";
        system("pause");
        system("cls");
    }

}
void Library::returnBook()
{
    FILE *fp=NULL; 
     fp=fopen("book1.dat","ab+");  
    string num;
    cout << "  *****************************还书界面*****************************\n\n";
    cout << "输入要还的书籍的编号:";
    cin >> num;
    int index;
    index = indexOfNum(num);
    if (index != -1)
    {
        cout << "要还的书籍的信息为:\n";
        books[index].showInfo();
        books[index].setBorrow_flag(false);
        readers[books[index].getReader()]--;
        cout << "还书成功。\n";
        system("pause");
        system("cls");
    }
    else
    {
        cout << "还书失败,请检查书籍编号是否输入错误!\n";
        system("pause");
        system("cls");
    }
    fclose(fp);
}

void Library::checkbook()
{
    FILE *fp=NULL; 
     fp=fopen("book1.dat","ab+");  
    string num,name;
    int index,i;
    cout << "  *****************************查询界面*****************************\n\n";
    cout << "图书查询方式:1.编号 2.名称" ;
    cout << "请选择查询方式:" ;
    cin >> i; 
    if (i==1){
    cout << "输入要查询的书籍的编号:";
    cin >> num;
    index = indexOfNum(num);
    if (index != -1) {
        if (books[index].isBorrow()) {
            cout << "图书信息:"; 
            cout << "书籍已经被借出。\n";
            books[index].showInfo();
            string reader, data;
            system("pause");
            system("cls");
        }
        else
        {
            cout << "图书的信息:\n";
            cout << "书籍未被借出。\n";
            books[index].showInfo();
            string reader, data;
            system("pause");
            system("cls");
            }
            
        }
    else
    {
        cout << "查询失败。未找到编号为" << num << "的书籍.\n";
        system("pause");
        system("cls");
    }
}
else if(i==2){
    cout << "输入要查询的书籍的名称:";
    cin >> name;
    index = indexOfNum(name);
    if (index != -1) {
        if (books[index].isBorrow()) {
            cout << "图书信息:"; 
            cout << "书籍已经被借出。\n";
            books[index].showInfo();
            string reader, data;
            system("pause");
            system("cls");
        }
        else
        {
            cout << "图书的信息:\n";
            books[index].showInfo();
            string reader, data;
            system("pause");
            system("cls");
            }
            
        }
    else
    {
        cout << "查询失败。未找到编号为" << num << "的书籍.\n";
        system("pause");
        system("cls");
    }
}
    fclose(fp);
}





int Library::indexOfNum(string num)
{
    int i;
    for (i = 0; i < books.size(); i++)
    {
        if (books[i].getNum() == num)
            return i;
    }
    return -1;
}

Library l;

void menu()
{
    int temp;
    
    while (1)
    {
        cout << "___________________________ 图书馆管理系统____________________________\n";
        cout << "                  ┏━━━━━━━━━━━━━┓                      \n";
        cout << "                  ┃ [0]退出系统。            ┃                      \n";
        cout << "                  ┃ [1]增加图书。            ┃                      \n";
        cout << "                  ┃ [2]删除图书。            ┃                      \n";
        cout << "                  ┃ [3]借阅图书。            ┃                      \n";
        cout << "                  ┃ [4]归还图书。            ┃                      \n";
        cout << "                  ┃ [5]查询图书。            ┃                      \n";
        cout << "                  ┗━━━━━━━━━━━━━┛                      \n";
        cout << "输入要进行的操作:";
        cin >> temp;
        switch (temp) {
        case 1:
            system("cls");
            l.addBook(); 
            break;
        case 2:system("cls");
            l.deleteBook(); 
            break;
        case 3:system("cls");
            l.brrowBook(); 
            break;
        case 4:system("cls");
            l.returnBook(); 
            break;
        case 5:system("cls");
            l.checkbook();
            break;
        case 0:
            
            exit(1);
            break;
        default:
            cout << "输入错误!" << endl;
            system("pause");
            system("cls");
        }
    }
}
int main()
{
    color(3); 
    menu();
    return 0;
}


参考GPT和自己的思路:要实现保存数据的功能,你可以使用文件来存储数据。在程序结束前,将所有的数据保存到文件中。然后,下次程序启动时,从文件中读取数据并加载到内存中。在你的代码中,可以在以下位置添加读写文件的代码:

在 Library 类的构造函数中读取文件并加载数据。
在 Library 类的析构函数中保存数据到文件。
以下是在你的代码中添加读写文件的建议:

在 Library 类中添加以下代码:

class Library
{
public:
// 书籍库
list<item> itemList;
Library() {
    // 初始化 currentNum 和 brrowNum
    currentNum = 0;
    brrowNum = 0;

    // 读取数据并加载到内存中
    loadBooksFromFile();
}

~Library() {
    // 保存数据到文件中
    saveBooksToFile();
}

// ...
private:
// ...
void loadBooksFromFile() {
    FILE* fp = fopen("books.dat", "rb");
    if (fp == NULL) {
        return;
    }

    while (!feof(fp)) {
        Book b;
        fread(&b, sizeof(b), 1, fp);
        if (b.getName().empty()) {
            break;
        }
        books.push_back(b);
        currentNum++;
    }

    fclose(fp);
}

void saveBooksToFile() {
    FILE* fp = fopen("books.dat", "wb");
    if (fp == NULL) {
        return;
    }

    for (int i = 0; i < books.size(); i++) {
        fwrite(&books[i], sizeof(books[i]), 1, fp);
    }

    fclose(fp);
}
};

在这个例子中,我们使用二进制文件来存储数据。在 loadBooksFromFile 函数中,我们打开文件,读取 Book 对象并将其添加到 books 容器中。在 saveBooksToFile 函数中,我们遍历 books 容器,将每个 Book 对象写入文件中。

请注意,我们在检查文件是否为空之前使用 feof 函数读取数据。feof 函数返回 true 表示已经读到了文件的结尾。这意味着我们可以在文件的末尾写入一些额外的数据,以便在下次打开文件时读取到它们。

另外,你需要将 Book 类的成员变量改为公共成员变量,否则将无法使用 fwrite 和 fread 函数将 Book 对象写入或从文件中读取。你可以通过将 Book 类的所有成员变量声明为 public 来实现:

class Book : public item
{
public:
string name;
string num;
string auther;
string price;
string number;
bool borrow_flag;
string reader;
int lcn;
string data;
// ...
Book() {}

Book(string name, string num, string auther, string price, string number, bool borrow_flag, string reader, int lcn, string data)
: name(name), num(num), auther(auther), price(price), number(number), borrow_flag(borrow_flag), reader(reader), lcn(lcn), data(data) {}

friend ostream& operator<<(ostream& os, const Book& book) {
    os << book.name << " " << book.num << " " << book.auther << " " << book.price << " " << book.number << " " << book.borrow_flag << " " << book.reader << " " << book.lcn << " " << book.data;
    return os;
}

friend istream& operator>>(istream& is, Book& book) {
    is >> book.name >> book.num >> book.auther >> book.price >> book.number >> book.borrow_flag >> book.reader >> book.lcn >> book.data;
    return is;
}
};

参考GPT和自己的思路:

要让这个图书管理系统输入的数据能够保留下来,可以采用以下两种方法:

  1. 使用文件存储:

在程序中,可以将输入的数据存储到文件中,在下次打开程序时,读取文件中保存的数据,然后在程序中显示出来。可以使用以下函数来进行文件的操作:

  • fopen():打开文件。
  • fread():读取文件。
  • fwrite():写入文件。
  • fclose():关闭文件。

示例代码:

// 在添加图书时将数据写入文件book1.dat
FILE *fp = fopen("book1.dat", "ab+");
fwrite(&b, sizeof(Book), 1, fp);
fclose(fp);

// 在程序启动时将数据读取出来
FILE *fp = fopen("book1.dat", "rb");
while (fread(&b, sizeof(Book), 1, fp) > 0) {
    books.push_back(b);
    if (!b.isBorrow()) {
        currentNum++;
    } else {
        brrowNum++;
    }
}
fclose(fp);
  1. 使用数据库存储:

在程序中,可以将输入的数据存储到数据库之中,在下次打开程序时,读取数据库中保存的数据,然后在程序中显示出来。可以使用以下数据库来进行数据库的操作:

  • MySQL
  • SQLite
  • Oracle

示例代码:

```
// 在添加图书时将数据保存到数据库中
mysql_query(mysql, "set names utf8");
mysql_query(mysql, "insert into Book values(...)");
mysql_close(mysql);

// 在程序启动时将数据从数据库中读取出来
mysql_query(mysql, "set names utf8");
mysql_query(mysql, "select * from Book");
MYSQL_RES *

为了能够保存每次输入的图书信息,可以使用文件存储的方式。C++中提供了文件操作的库函数,可以实现文件的读写操作。下面是一个简单的文件操作的示例,可以将每次输入的图书信息写入到一个文本文件中,程序关闭再打开时可以读取文件中的数据,实现数据的持久化保存。

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

class Book
{
public:
    string name;
    string num;
    string author;
    string price;
    string number;
};

void saveBook(Book b)
{
    ofstream outFile;
    outFile.open("book.txt", ios::app); //append to the end of the file
    if (outFile.is_open())
    {
        outFile << b.name << "," << b.num << "," << b.author <<"," << b.price <<","<< b.number<< endl;
        outFile.close();
    }
    else
    {
        cout << "Unable to open file!" << endl;
    }
}

void readBook()
{
    ifstream inFile;
    inFile.open("book.txt", ios::in);
    if (inFile.is_open())
    {
        string line;
        while (getline(inFile, line))
        {
            cout << line << endl;
        }
        inFile.close();
    }
    else
    {
        cout << "Unable to open file!" << endl;
    }
}

int main()
{
    Book b;
    cout << "Please enter book name: ";
    cin >> b.name;
    cout << "Please enter book number: ";
    cin >> b.num;
    cout << "Please enter book author: ";
    cin >> b.author;
    cout << "Please enter book price: ";
    cin >> b.price;
    cout << "Please enter book number: ";
    cin >> b.number;

    saveBook(b);

    readBook();

    return 0;
}

上面的代码中,我们使用了ofstreamifstream两个类来分别实现文件的写入和读取操作。在保存图书信息时,使用了ofstream::open()函数打开文件,ios::app参数表示以追加的方式打开文件,即每次写入都是在文件末尾进行。在读取图书信息时,使用了ifstream::open()函数打开文件,ios::in参数表示以读取的方式打开文件,可以逐行读取文件中的数据。

在实际使用中,我们可以将保存和读取函数封装到图书管理系统的类中,以实现数据的持久化。同时,我们还可以使用更加高效的二进制文件存储方式来保存数据,以提高程序的执行效率。

参考GPT和自己的思路,在C语言的图书管理系统中,可以使用文件来保存图书信息以便于程序关闭再打开时可以继续使用之前保存的信息。可以使用标准C库中的文件操作函数来实现这一功能。具体实现步骤如下:

1.在程序开始时,尝试打开一个用于保存数据的文件,比如 "book_data.txt"。
2.如果文件不存在,则新建一个文件。
3.如果文件已存在,则读取文件中已保存的图书信息,将其存储到内存中。
4.程序运行时,用户输入图书信息,将其存储到内存中,并将其写入到文件中。
5.程序关闭时,将所有图书信息写入到文件中,以便于下次程序启动时可以继续使用之前保存的信息。
以下是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <conio.h>

#define MAX_BOOK_NUM 1000   // 最大书籍数量
#define MAX_NAME_LEN 100    // 书名最大长度
#define MAX_NUM_LEN 20      // 书号最大长度
#define MAX_AUTHOR_LEN 50   // 作者名最大长度
#define MAX_DATA_LEN 20     // 借书日期最大长度
#define MAX_READER_LEN 50   // 读者名最大长度
#define MAX_LINE_LEN (MAX_NAME_LEN + MAX_NUM_LEN + MAX_AUTHOR_LEN + 3)

// 书籍信息结构体
typedef struct {
    char name[MAX_NAME_LEN];    // 书名
    char num[MAX_NUM_LEN];      // 书号
    char author[MAX_AUTHOR_LEN];// 作者名
    float price;                // 价格
    int number;                 // 数量
    int borrow_flag;            // 借出标志
    char reader[MAX_READER_LEN];// 借书人名
    char data[MAX_DATA_LEN];    // 借书日期
} Book;

// 图书馆结构体
typedef struct {
    Book books[MAX_BOOK_NUM];   // 所有书籍
    int currentNum;             // 库存书籍数目(不包括借出的)
    int brrowNum;               // 借出书籍数目
} Library;

// 显示控制台字体颜色
void color(int x) {
    if (x >= 0 && x <= 15)
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
    else
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}

// 将图书信息写入文件
void writeToFile(Book book) {
    FILE *fp;
    fp = fopen("book_data.txt", "a+");
    if (fp != NULL) {
        fprintf(fp, "%s %s %s %.2f %d %d %s %s\n", book.name, book.num, book.author, book.price, book.number, book.borrow_flag, book.reader, book.data);
        fclose(fp);
    }
}

// 从文件中读取图书信息
void readFromFile(Library *library) {
    FILE *fp;
    char line[MAX_LINE_LEN];
    char *name, *num, *author, *reader, *data;
    float price;
    int count = 0;
    fp = fopen("book.txt", "r");
    if (fp == NULL) {
        printf("Failed to open file.\n");
        return;
    }

    while (fgets(line, MAX_LINE_LEN, fp) != NULL) {
        // 使用 strtok 函数解析每一行数据
        name = strtok(line, ",");
        num = strtok(NULL, ",");
        author = strtok(NULL, ",");
        price = atof(strtok(NULL, ","));
        reader = strtok(NULL, ",");
        data = strtok(NULL, ",");
    
        // 创建图书节点并添加到链表中
        Book *book = createBook(name, num, author, price, reader, data);
        addBook(library, book);
        count++;
    }

    fclose(fp);
    printf("Read %d books from file.\n", count);
}
不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

哥哥要将输入的数据保存下来并在程序关闭后仍能查询到它们,你需要使用一种持久化存储的方法。有几种不同的方法可以实现这一点:

  • 数据库:将数据存储在数据库中是一种常见的方法。你可以使用关系型数据库(如MySQL、PostgreSQL)或NoSQL数据库(如MongoDB、Cassandra),根据你的需求选择合适的数据库类型。在程序启动时,你可以连接到数据库并读取以前保存的数据,然后在程序关闭时再次将更新保存到数据库中。
  • 文件系统:另一种方法是将数据保存为文件。你可以使用文本文件、JSON文件或XML文件等格式来保存数据。在程序启动时,你可以读取文件中的数据,并在程序关闭时将更新写入文件中。
  • 缓存:如果你只需要临时存储数据,或者你的数据量很小,那么缓存可能是一个更好的选择。你可以使用内存缓存(如Redis、Memcached)或本地缓存(如Guava Cache、Ehcache)来存储数据。在程序启动时,你可以从缓存中读取数据,并在程序关闭时再次将更新存储到缓存中。

无论你选择哪种方法,都需要确保你的数据是安全的,并且可以在程序启动和关闭时正确地加载和保存。

  • 你要实现将C语言图书管理系统输入的数据保存下来,关闭程序再打开时仍能查询到上次录入的图书,您可以考虑以下两种方式之一:
  1. 使用文件存储数据:您可以在程序中使用一个文件来存储图书信息。当用户录入新的图书信息后,程序会将这些信息写入文件中保存。当用户再次打开程序时,程序会从文件中读取以前保存的数据,并将其显示在界面上。

示例代码:

```c
#include <stdio.h>

struct Book {
    char title[50];
    char author[50];
    char subject[100];
    int book_id;
};

int main() {
    FILE *fp;
    struct Book book;

    // 打开文件,如果不存在则创建
    fp = fopen("books.dat", "ab+");

    // 读取文件中保存的图书信息并显示
    while (fread(&book, sizeof(struct Book), 1, fp) == 1) {
        printf("Title: %s\n", book.title);
        printf("Author: %s\n", book.author);
        printf("Subject: %s\n", book.subject);
        printf("Book ID: %d\n", book.book_id);
    }

    // 添加新的图书信息
    printf("Enter book title: ");
    fgets(book.title, 50, stdin);
    printf("Enter book author: ");
    fgets(book.author, 50, stdin);
    printf("Enter book subject: ");
    fgets(book.subject, 100, stdin);
    printf("Enter book ID: ");
    scanf("%d", &book.book_id);

    // 将新的图书信息写入文件
    fwrite(&book, sizeof(struct Book), 1, fp);

    // 关闭文件
    fclose(fp);

    return 0;
}
  1. 使用数据库存储数据:您还可以在程序中使用一个数据库来存储图书信息。当用户录入新的图书信息后,程序会将这些信息保存到数据库中。当用户再次打开程序时,程序会从数据库中读取以前保存的数据,并将其显示在界面上。

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>

static int callback(void *data, int argc, char **argv, char **col_name) {
    int i;
    for (i = 0; i < argc; i++) {
        printf("%s: %s\n", col_name[i], argv[i] ? argv[i] : "NULL");
    }
    printf("\n");
    return 0;
}

int main(int argc, char **argv) {
    sqlite3 *db;
    char *sql;
    char *err_msg = 0;

    // 打开数据库,如果不存在则创建
    int rc = sqlite3_open("books.db", &db);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return 1;
    }

    // 创建表格(如果不存在)
    sql = "CREATE TABLE IF NOT EXISTS Books (" \
          "id INTEGER PRIMARY KEY AUTOINCREMENT," \
          "title TEXT NOT NULL," \
          "author TEXT NOT NULL," \
          "subject TEXT NOT NULL," \
          "book_id INT NOT NULL);";
    rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error: %s\n", err_msg);
        sqlite3_free(err_msg);
        sqlite3_close(db);
        return 1;
    }

    // 查询表格中保存的图书信息并显示
    sql = "SELECT * FROM Books;";
    rc = sqlite3_exec(db, sql, callback, 0, &err_msg);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "SQL error: %s\n", err_msg);
        sqlite3_free(err_msg);
    }

    // 添加新的图书信息
    char title[50], author[50], subject[100];
    int book_id;
    printf("Enter book title: ");
    fgets(title, 50, stdin);
    printf("Enter book author: ");
    fgets(author, 50, stdin);
    printf("Enter book subject: ");
    fgets(subject, 100, stdin);
    printf("Enter book ID: ");
    scanf("%d", &book_id);

    // 插入新的