我将此代码作为我的默认构造函数。我正在尝试从我的文件(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发布于 2021-09-16 21:57:40
operator>>停止读取空格,这在本例中不是您需要的。
我建议使用std::getline()读取整行,然后使用std::string::rfind()查找数字前面的最后一个空格字符,然后使用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。让标准库为您处理内存管理:
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...https://stackoverflow.com/questions/69215139
复制相似问题