c++图书管理系统中学生和图书信息文件的存入读取
时间: 2024-04-22 12:23:50 浏览: 24
对于C++图书管理系统中学生和图书信息的存储和读取,可以通过文件操作来实现。以下是一个简单的示例:
```C++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// 学生类
class Student {
public:
string name; // 学生姓名
string id; // 学生学号
};
// 图书类
class Book {
public:
string name; // 图书名称
string id; // 图书编号
};
// 存储学生信息到文件中
void saveStudentsToFile(Student students[], int count) {
ofstream fout("students.txt"); // 打开文件
if (fout.is_open()) {
for (int i = 0; i < count; i++) {
fout << students[i].name << " " << students[i].id << endl;
}
fout.close(); // 关闭文件
}
}
// 从文件中读取学生信息
void loadStudentsFromFile(Student students[], int count) {
ifstream fin("students.txt"); // 打开文件
if (fin.is_open()) {
for (int i = 0; i < count; i++) {
fin >> students[i].name >> students[i].id;
}
fin.close(); // 关闭文件
}
}
// 存储图书信息到文件中
void saveBooksToFile(Book books[], int count) {
ofstream fout("books.txt"); // 打开文件
if (fout.is_open()) {
for (int i = 0; i < count; i++) {
fout << books[i].name << " " << books[i].id << endl;
}
fout.close(); // 关闭文件
}
}
// 从文件中读取图书信息
void loadBooksFromFile(Book books[], int count) {
ifstream fin("books.txt"); // 打开文件
if (fin.is_open()) {
for (int i = 0; i < count; i++) {
fin >> books[i].name >> books[i].id;
}
fin.close(); // 关闭文件
}
}
int main() {
// 定义学生数组和图书数组
Student students[3] = {{"张三", "201901"}, {"李四", "201902"}, {"王五", "201903"}};
Book books[3] = {{"C++ Primer", "1001"}, {"Effective C++", "1002"}, {"STL源码剖析", "1003"}};
// 存储学生信息和图书信息到文件中
saveStudentsToFile(students, 3);
saveBooksToFile(books, 3);
// 从文件中读取学生信息和图书信息
Student newStudents[3];
Book newBooks[3];
loadStudentsFromFile(newStudents, 3);
loadBooksFromFile(newBooks, 3);
// 输出读取的学生信息和图书信息
for (int i = 0; i < 3; i++) {
cout << newStudents[i].name << " " << newStudents[i].id << endl;
cout << newBooks[i].name << " " << newBooks[i].id << endl;
}
return 0;
}
```
在上面的示例中,我们定义了一个`Student`类和一个`Book`类,分别表示学生和图书。然后,我们通过文件操作中的`ofstream`和`ifstream`类来实现将学生和图书信息存储到文件中和从文件中读取信息。存储操作通过写入到文件中,读取操作通过从文件中读取并解析数据来实现。最后,我们通过输出来验证读取到的数据是否正确。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)