从文件C ++读取类对象 [英] Read class objects from file c++

查看:139
本文介绍了从文件C ++读取类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从文件中读取类对象,但是我不知道怎么做。

I need to read class objects from file, but I don't know how.

这里我有一个人类

class People{
public:

string name;
string surname;
int years;
private:

People(string a, string b, int c):
name(a),surname(b),years(c){}
};

现在,我想从.txt文件中读取人物,并将其存储到People类中。

Now I would like to read peoples from .txt file and store them to objects of a class People.

例如,这就是我的.txt文件的样子:

For instance, this is how my .txt file looks like:

John Snow 32
Arya Stark 19
Hodor Hodor 55
Ned Stark 00

我认为最好的方法是创建4个对象的数组。如果我假设正确,但我不知道该怎么做,我需要逐字逐行阅读。

I think the best way to do this would be to create array of 4 objects. I need to read word by word and line after line, if I assume correctly but I don't know how...

推荐答案

执行此操作的方法是为您的班级编写一种存储格式,例如,如果我这样做,我将像您一样存储信息

The way to do this would be to compose a storage format for your class, for instance if I were to do this I would store the information just like you did

John Snow 32
Arya Stark 19
Hodor Hodor 55
Ned Stark 00

要阅读本文,可以执行以下操作

To read this in you could do the following

ifstream fin;
fin.open("input.txt");
if (!fin) {
    cerr << "Error in opening the file" << endl;
    return 1; // if this is main
}

vector<People> people;
People temp;
while (fin >> temp.name >> temp.surname >> temp.years) {
    people.push_back(temp);
}

// now print the information you read in
for (const auto& person : people) {
    cout << person.name << ' ' << person.surname << ' ' << person.years << endl;
}

要将其写入文件,您可以执行以下操作

To write it to a file you could do the following

static const char* const FILENAME_PEOPLE = "people.txt";
ofstream fout;
fout.open(FILENAME_PEOPLE); // be sure that the argument is a c string
if (!fout) {
    cerr << "Error in opening the output file" << endl;

    // again only if this is main, chain return codes or throw an exception otherwise
    return 1; 
}

// form the vector of people here ...
// ..
// ..

for (const auto& person : people) {
    fout << people.name << ' ' << people.surname << ' ' << people.years << '\n';
}

如果您不熟悉向量是什么 vector s是存储对象的数组的推荐方法,这些对象可以在C ++中动态增长。 vector 类是C ++标准库的一部分。而且,由于您正在从文件中读取数据,因此您不应事先假设要在文件中存储多少个对象。

If you are not familiar with what a vector is vectors are the recommended way to store an array of objects that can grow dynamically in C++. The vector class is a part of the C++ standard library. And since you are reading in from a file you should not make any assumptions about how many objects are going to be stored in the file ahead of time.

但是,以防万一您不熟悉我在上面的示例中使用的类和功能。这是一些链接

But just in case you are not familiar with the classes and features I used in the example above. Here are some links

vector http://en.cppreference.com/w/cpp/container/vector

ifstream http://en.cppreference.com/w/cpp/io/basic_ifstream

基于范围的循环 http://en.cppreference.com/w/cpp/language/range-for

auto http://en.cppreference.com/w/cpp/language/auto

这篇关于从文件C ++读取类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆