迭代txt文件中的对象数组

时间:2017-05-18 08:48:38

标签: c++ arrays file class oop

我的文件包含以逗号分隔的记录:

cities.txt:

1,NYC
2,ABQ
...

我想迭代每一行:ID和名称。我创建了代码:

#include <iostream>
#include <string>
using namespace std;

class City {
    int id;
    string name;

public:
    City() {}
    City(int id, int name)
    {
        this->id = id;
        this->name = name;
    }

    void load_file()
    {
        ifstream v_file("cities.txt");
        if (v_file.is_open()) {
            while (!v_file.eof()) {
            //...
            }
        }
        v_file.close();
    }
}

int main()
{
    City array_city[1000];

    array_city.load_file();

    return 0;
}

你能告诉我如何将所有行加载到数组array_city并迭代它吗?我不知道在while方法的load_file块中放置什么内容。我不知道天气,方法load_file应该有void类型。不幸的是,我必须在阵列上进行。

1 个答案:

答案 0 :(得分:1)

在while循环中使用EOF不是一个好主意。阅读Why is iostream::eof inside a loop condition considered wrong?

中的更多内容

中,向量应优先于数组。但是,您的老师知道更多建议在这里使用数组。出于这个原因,我提供了一个带阵列的解决方案:

  1. 逐行阅读文件
  2. 提取id和字符串
  3. 将其分配给数组的第i个单元格
  4. 代码:

    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    class City {
        int id;
        string name;
    
    public:
        City() {}
        City(int id, string name) : id(id), name(name)
        {
        }
        void print()
        {
            cout << "ID = " << id << ", name = " << name << endl;
        }
    };
    
    void load_file(City* cities, const int n)
    {
        ifstream v_file("cities.txt");
        if (v_file.is_open()) {
            int number, i = 0;
            string str;
            char c;
            while (v_file >> number >> c >> str && c == ',' && i < n)
            {
                //cout << number << " " << str << endl;
                cities[i++] = {number, str};
            }
        }
        v_file.close();
    }
    
    int main()
    {
        City cities[4]; // assuming there are 4 cities in the file
        load_file(cities, 4);
        for(unsigned int i = 0; i < 4; ++i)
            cities[i].print();
    
        return 0;
    }
    

    std::vector相同的解决方案,如果您有兴趣的话。 =)如果您没有接受过关于他们的教育,我建议您跳过这部分,然后在课程中这样做时再回来。

    使用City的向量。 Read the file line by line,并通过构建一个类的实例,将每行读回向量,然后就完成了!

    示例:

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    class City {
        int id;
        string name;
    
    public:
        City() {}
        City(int id, string name) : id(id), name(name)
        {
        }
        void print()
        {
            cout << "ID = " << id << ", name = " << name << endl;
        }
    };
    
    void load_file(vector<City>& cities)
    {
        ifstream v_file("cities.txt");
        if (v_file.is_open()) {
            int number;
            string str;
            char c;
            while (v_file >> number >> c >> str && c == ',' && i < n)
            {
                //cout << number << " " << str << endl;
                cities.push_back({number, str});
            }
        }
        v_file.close();
    }
    
    int main()
    {
        vector<City> cities;
        load_file(cities);
        for(unsigned int i = 0; i < cities.size(); ++i)
            cities[i].print();
    
        return 0;
    }
    

    输入:

    Georgioss-MacBook-Pro:~ gsamaras$ cat cities.txt 
    1,NYC
    2,ABQ
    3,CCC
    4,DDD
    

    输出:

    Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall -std=c++0x main.cpp 
    Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
    ID = 1, name = NYC
    ID = 2, name = ABQ
    ID = 3, name = CCC
    ID = 4, name = DDD