我如何合并 getline 函数以便我的程序能够正确地从文件中读取数据?

How do I incorporate the getline function so my program will read data from the file correctly?

我将这段代码作为我的默认构造函数。我正在尝试从我的文件 (census2020_data.txt) 中正确读取所有值。该文件包含字母和数字,由州和代表人口的值组成。数据文件中的一个值有空格(它是多个单词),这使我的程序无法正确从文件中读取。

如何合并 getline() 函数来帮助正确读取我的文件?我将在下面附上填充内容作为示例。

构造函数:

state_class::state_class()
{
    //intially count, capacity, and pop_DB are initialized with the following values:
    count = 0;
    capacity = 5;
    ifstream in;
    pop_DB = new population_record[capacity];
    
    in.open("census2020_data.txt");
    //check if file can be opened

    while (!in.eof())
    {
        if (Is_Full())
        {
            double_size();
        }
        else
        {
            in >> pop_DB[count].state_name >> pop_DB[count].population;
            count++;
        }
    }

    in.close();
}

census2020_data.txt:

Alaska 710231
Arizona 6392017
District of Columbia 601723
Arkansas 2915918
California 37253956
Connecticut 3574097
Delaware 897934
Florida 18801310
Indiana 6483802
Maine 1328361
Oregon 3831074
Pennsylvania 12702379
Rhode Island 1052567
South Carolina 4625364
Maryland 5773552
Massachusetts 6547629
Michigan 9883640
Colorado 5029196
Minnesota 5303925
Mississippi 2967297
Iowa 3046355
Kansas 2853118
Kentucky 4339367
Louisiana 4533372
Missouri 5988927
Montana 989415
South Dakota 814180
Tennessee 6346105
Texas 25145561
Utah 2763885
Vermont 625741
Nebraska 1826341
Nevada 2700551
New Hampshire 1316470
New Jersey 8791894
Georgia 9687653
Hawaii 1360301
Idaho 1567582
Illinois 12830632
New Mexico 2059179
New York 19378102
North Carolina 9535483
North Dakota 672591
Ohio 11536504
Oklahoma 3751351
Virginia 8001024
Washington 6724540
West Virginia 1852994
Wisconsin 5686986
Wyoming 563626
Puerto Rico 3725789
Alabama 4779736

operator>> 停止阅读白色space,在这种情况下这不是您所需要的。

我建议使用 std::getline() 读取整行,然后使用 std::string::rfind() 找到数字前的最后一个 space 字符,然后使用 std::string::substr()从数字中拆分文本。例如:

state_class::state_class()
{
    //intially count, capacity, and pop_DB are initialized with the following values:
    count = 0;
    capacity = 5;
    pop_DB = new population_record[capacity];
    
    //check if file can be opened
    std::ifstream in("census2020_data.txt");

    std::string line;
    while (std::getline(in, line))
    {
        if (Is_Full())
            double_size();

        auto pos = line.rfind(' ');
        pop_DB[count].state_name = line.substr(0, pos);
        pop_DB[count].population = std::stoi(line.substr(pos+1));
        ++count;
    }
}

也就是说,与其手动维护您自己的 population_record[] 数组,不如考虑使用 std::vector<population_record>。让标准库为您处理内存管理:

private:
    std::vector<population_record> pop_DB;

state_class::state_class()
{
    //check if file can be opened
    std::ifstream in("census2020_data.txt");

    std::string line;
    while (std::getline(in, line))
    {
        auto pos = line.rfind(' ');
        population_record rec;
        rec.state_name = line.substr(0, pos);
        rec.population = std::stoi(line.substr(pos+1));
        pop_DB.push_back(rec);
    }
}

// use pop_DB.size() and pop_DB[index] as needed...

我什至建议实施 operator>> 以直接从 ifstream:

读取 population_record 条目
std::istream& operator>>(std::istream& in, population_record &rec)
{
    std::string line;
    if (std::getline(in, line))
    {
        auto pos = line.rfind(' ');
        rec.state_name = line.substr(0, pos);
        rec.population = std::stoi(line.substr(pos+1));
    }
    return in;
}

...

private:
    std::vector<population_record> pop_DB;

state_class::state_class()
{
    //check if file can be opened
    std::ifstream in("census2020_data.txt");

    population_record rec;
    while (in >> rec) {
        pop_DB.push_back(rec);
    }
}

// use pop_DB.size() and pop_DB[index] as needed...