设计一个学生类,其中包含学号、姓名、成绩等数据成员,创建学生对象并且倒入到文件file.txt,然后由文件读取到另一个学生对象并输出,试编程实现。
2012-12-28 10:08
1631 查看
《转自论坛》
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cassert>
#include <cstdlib>
using namespace std;
class Student {
string number;
string name;
int score;
public:
Student(const char *number0, const char *name0, int score0);
bool writeToFile(const char *path) const ;
const Student readFromFile(const char *path) const;
void display() const;
};
Student::Student(const char *number0, const char *name0, int score0): number(number0), name(name0), score(score0)
{
}
bool Student::writeToFile(const char *path) const
{
assert(path);
ofstream file(path);
if(!file)
return false;
file << "number:" << number << endl;
file << "name:" << name << endl;
file << "score:" << score<< endl;
file.close();
return true;
}
const Student Student::readFromFile(const char *path) const
{
assert(path);
string number;
string name;
int score;
ifstream file(path);
if(!path)
throw runtime_error("open file to read failed");
string s;
while(file >> s) {
int pos = s.find(":");
if(s.substr(0, pos).compare("number") == 0)
number = s.substr(pos + 1);
if(s.substr(0, pos).compare("name") == 0)
name = s.substr(pos + 1);
if(s.substr(0, pos).compare("score") == 0)
score = atoi(s.substr(pos + 1).c_str());
}
return Student(number.c_str(), name.c_str(), score);
}
void Student::display() const
{
cout << "number: " << number << endl;
cout << "name: " << name << endl;
cout << "score: " << score << endl;
}
int main()
{
const char *filePath = "./file.txt";
Student stu("037165", "张三", 50);
stu.writeToFile(filePath);
Student stuCopy = stu.readFromFile(filePath);
stuCopy.display();
}
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cassert>
#include <cstdlib>
using namespace std;
class Student {
string number;
string name;
int score;
public:
Student(const char *number0, const char *name0, int score0);
bool writeToFile(const char *path) const ;
const Student readFromFile(const char *path) const;
void display() const;
};
Student::Student(const char *number0, const char *name0, int score0): number(number0), name(name0), score(score0)
{
}
bool Student::writeToFile(const char *path) const
{
assert(path);
ofstream file(path);
if(!file)
return false;
file << "number:" << number << endl;
file << "name:" << name << endl;
file << "score:" << score<< endl;
file.close();
return true;
}
const Student Student::readFromFile(const char *path) const
{
assert(path);
string number;
string name;
int score;
ifstream file(path);
if(!path)
throw runtime_error("open file to read failed");
string s;
while(file >> s) {
int pos = s.find(":");
if(s.substr(0, pos).compare("number") == 0)
number = s.substr(pos + 1);
if(s.substr(0, pos).compare("name") == 0)
name = s.substr(pos + 1);
if(s.substr(0, pos).compare("score") == 0)
score = atoi(s.substr(pos + 1).c_str());
}
return Student(number.c_str(), name.c_str(), score);
}
void Student::display() const
{
cout << "number: " << number << endl;
cout << "name: " << name << endl;
cout << "score: " << score << endl;
}
int main()
{
const char *filePath = "./file.txt";
Student stu("037165", "张三", 50);
stu.writeToFile(filePath);
Student stuCopy = stu.readFromFile(filePath);
stuCopy.display();
}
相关文章推荐
- [4.21]编写一个程序,已有若干个学生的数据,包括学号、姓名、成绩、要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)。
- 创建一个学生类(Student),包括学号和成绩,编程输入和显示学生的信息,建立一个人类(Person),包含姓名、性别和年龄,并作为学生类的基类
- 从一个名为 in_file.txt 的文本文件中读取单词,然后把每个词写到一个名为out_file.txt的输出文件中 并且每个词之间用空格分开
- 创建一个学生类(Student),包括学号成绩,编程输入和显示学生的信息。建立一个人类(Person),包含姓名、性别和年龄,并作为学生类的基类
- 输入N个学生的信息(包括:学号,姓名,5科成绩,总分),统计所有学生的总分,存入二进制文件stu_sc.dat中。然后读取该文件,查找总分最高的学生,并输出该生的所有信息。
- 要求从用户输入的多行文本中提取学生的姓名、学号及登录日期,并封装到 Student 类中作为类的私有属性。创建一个 Student 类型的对象数组,对学号进行 升序排序并输出
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 要求从用户输入的多行文本中提取学生的姓名、学号及登录日期,并封装到 Student 类中作为类的私有属性。创建一个Student类型的对象数组,对学号进行 升序排序并输出。
- 建立一个对象数组,内放5个学生数据(学号、成绩),用指针指向数组首元素,输出学生的数据
- 定义一个描述学生(Student)基本情况的类,数据成员包括姓名(name)、学号(num)、数学成绩(mathScore)、英语成绩(englishScore)、人数(count)、数学总成绩(ma
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- Chapter9——建立一个对象数组,内放5个学生的数据(学号,成绩),设立一个函数max,用指向对象的指针做函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
- 有一个结构体变量stu,内含学生学号,姓名和三门课成绩。要求在main函数中为各位成员赋值,在另一函数print中将他们输出。
- 定义一个学生结构体类型struct student,里边含有学号,姓名,分数,定义结构体数组struct student s[3],给数组赋初值后, 写入文件,然后通过lseek偏移到开头,然后再读取
- 定义一个描述学生(Student)基本情况的类,数据成员包括姓名(name)、学号(num)、数学成绩(mathScore)、英语成绩(englishScore)、人数(count)、数学总成绩(ma
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 建立一个对象数组,内放5个学生数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并从输出学号。
- 第四章作业:4.17:建立一个对象数组,内放6个学生的数据(学号,成绩),用指针向数组首元素,输出第2,4,6个学生的数据。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。