定义一个学生类, 包含学生的学号、 姓名和年龄等基本信息, 将学生信息写入二进 制文件 student. dat 中, 实现对学生信息的显示、 查询和删除等基本功能。用c++
时间: 2024-02-03 12:13:51 浏览: 71
学生信息管理系统代码
下面是一个示例的学生类定义和实现:
```cpp
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int MAX_NAME_LEN = 20;
struct Student {
int id;
char name[MAX_NAME_LEN];
int age;
};
class StudentManager {
public:
void addStudent(int id, const char* name, int age);
void displayAllStudents();
void findStudentById(int id);
void removeStudentById(int id);
private:
fstream file;
void writeStudentToFile(const Student& student);
Student readStudentFromFile();
bool matchById(const Student& student, int id);
};
void StudentManager::addStudent(int id, const char* name, int age) {
Student student = {id};
strncpy(student.name, name, MAX_NAME_LEN - 1);
student.age = age;
writeStudentToFile(student);
}
void StudentManager::displayAllStudents() {
file.open("student.dat", ios::in | ios::binary);
Student student;
while (file.read(reinterpret_cast<char*>(&student), sizeof(Student))) {
cout << "id: " << student.id << ", name: " << student.name << ", age: " << student.age << endl;
}
file.close();
}
void StudentManager::findStudentById(int id) {
file.open("student.dat", ios::in | ios::binary);
Student student;
bool found = false;
while (file.read(reinterpret_cast<char*>(&student), sizeof(Student))) {
if (matchById(student, id)) {
cout << "id: " << student.id << ", name: " << student.name << ", age: " << student.age << endl;
found = true;
break;
}
}
if (!found) {
cout << "Student with id " << id << " not found" << endl;
}
file.close();
}
void StudentManager::removeStudentById(int id) {
file.open("student.dat", ios::in | ios::out | ios::binary);
Student student;
int pos = 0;
while (file.read(reinterpret_cast<char*>(&student), sizeof(Student))) {
if (matchById(student, id)) {
file.seekp(pos);
Student emptyStudent = {0};
file.write(reinterpret_cast<char*>(&emptyStudent), sizeof(Student));
cout << "Student with id " << id << " removed" << endl;
break;
}
pos = file.tellg();
}
if (file.eof()) {
cout << "Student with id " << id << " not found" << endl;
}
file.close();
}
void StudentManager::writeStudentToFile(const Student& student) {
file.open("student.dat", ios::out | ios::app | ios::binary);
file.write(reinterpret_cast<const char*>(&student), sizeof(Student));
file.close();
}
Student StudentManager::readStudentFromFile() {
Student student;
file.read(reinterpret_cast<char*>(&student), sizeof(Student));
return student;
}
bool StudentManager::matchById(const Student& student, int id) {
return student.id == id;
}
```
在这个实现中,我们使用了 `fstream` 类来读写二进制文件。具体来说,对于写入操作,我们使用了 `ios::out` 和 `ios::app` 模式打开文件,以便在文件末尾添加新的学生信息;对于读取操作,我们使用了 `ios::in` 模式打开文件。在读写操作中,我们使用了 `reinterpret_cast` 来进行指针类型的转换,以便正确地读写二进制数据。
另外,我们还实现了一些基本的功能,比如添加学生、显示所有学生、按照学号查找学生和按照学号删除学生。在查找和删除操作中,我们使用了一个辅助函数 `matchById` 来判断学生信息是否与给定的学号匹配。
最后,我们可以在 `main` 函数中测试我们的实现:
```cpp
int main() {
StudentManager manager;
manager.addStudent(1, "Alice", 20);
manager.addStudent(2, "Bob", 21);
manager.addStudent(3, "Charlie", 22);
cout << "All students:" << endl;
manager.displayAllStudents();
cout << "Find student with id 2:" << endl;
manager.findStudentById(2);
cout << "Remove student with id 3:" << endl;
manager.removeStudentById(3);
cout << "All students after removal:" << endl;
manager.displayAllStudents();
return 0;
}
```
输出结果应该为:
```
All students:
id: 1, name: Alice, age: 20
id: 2, name: Bob, age: 21
id: 3, name: Charlie, age: 22
Find student with id 2:
id: 2, name: Bob, age: 21
Remove student with id 3:
Student with id 3 removed
All students after removal:
id: 1, name: Alice, age: 20
id: 2, name: Bob, age: 21
```
阅读全文