迭代 txt 文件中的对象数组

Iterate over array of objects from txt file

本文关键字:对象 数组 txt 文件 迭代      更新时间:2023-10-16

我有文件,其中的记录用逗号分隔:

城市.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并迭代它吗?我不知道load_file方法中while块中放置什么。我不知道天气,load_file的方法应该有void类型。不幸的是,我必须在数组上执行此操作。

在 while 循环中使用 EOF 不是一个好主意。阅读更多 为什么循环条件内的 iostream::eof 被认为是错误的?


在 c++ 中,向量应该优先于数组。但是,您的老师知道更多建议在此处使用数组的内容。出于这个原因,我提供了一个带有数组的解决方案:

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

法典:

#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 的向量。逐行读取文件,并通过构造类的实例将读取的每一行推送回向量,您就完成了!

例:

#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