请问怎么把这个管理系统输入的数据保存下来,
关闭程序再打开时仍能查询到上次录入的图书啊?
#include
#include<string>
#include
#include
参考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和自己的思路:
要让这个图书管理系统输入的数据能够保留下来,可以采用以下两种方法:
在程序中,可以将输入的数据存储到文件中,在下次打开程序时,读取文件中保存的数据,然后在程序中显示出来。可以使用以下函数来进行文件的操作:
示例代码:
// 在添加图书时将数据写入文件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);
在程序中,可以将输入的数据存储到数据库之中,在下次打开程序时,读取数据库中保存的数据,然后在程序中显示出来。可以使用以下数据库来进行数据库的操作:
示例代码:
```
// 在添加图书时将数据保存到数据库中
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;
}
上面的代码中,我们使用了ofstream
和ifstream
两个类来分别实现文件的写入和读取操作。在保存图书信息时,使用了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);
}
不知道你这个问题是否已经解决, 如果还没有解决的话:哥哥要将输入的数据保存下来并在程序关闭后仍能查询到它们,你需要使用一种持久化存储的方法。有几种不同的方法可以实现这一点:
无论你选择哪种方法,都需要确保你的数据是安全的,并且可以在程序启动和关闭时正确地加载和保存。
示例代码:
```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;
}
示例代码:
#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);
// 插入新的