c++成绩管理系统文件写入
时间: 2024-04-19 10:20:11 浏览: 10
C++成绩管理系统文件写入是指将学生成绩信息保存到文件中的操作。下面是一个简单的示例代码,演示了如何实现成绩管理系统的文件写入功能:
```cpp
#include <iostream>
#include <fstream> // 包含文件操作相关的头文件
using namespace std;
// 定义学生类
class Student {
public:
string name;
int score;
Student(string n, int s) {
name = n;
score = s;
}
};
int main() {
// 创建学生对象
Student student1("张三", 90);
Student student2("李四", 80);
Student student3("王五", 70);
// 打开文件
ofstream outfile("scores.txt");
// 将学生信息写入文件
outfile << student1.name << " " << student1.score << endl;
outfile << student2.name << " " << student2.score << endl;
outfile << student3.name << " " << student3.score << endl;
// 关闭文件
outfile.close();
cout << "成绩信息已成功写入文件!" << endl;
return 0;
}
```
上述代码中,我们首先定义了一个`Student`类,表示学生对象,包含姓名和成绩两个属性。然后在`main`函数中创建了三个学生对象,并将它们的信息写入到名为"scores.txt"的文件中。最后输出提示信息表示写入成功。