gpt4 book ai didi

c++ - 如何将逗号分隔文件的第 n 列存储在数组中?

转载 作者:太空宇宙 更新时间:2023-11-03 10:44:35 24 4
gpt4 key购买 nike

<分区>

我有一个以下列格式存储数据的文件(这只是一个小示例):

AD,Andorra,AN,AD,AND,20.00,Andorra la Vella,Europe,Euro,EUR,67627.00
AE,United Arab Emirates,AE,AE,ARE,784.00,Abu Dhabi,Middle East,UAE Dirham,AED,2407460.00
AF,Afghanistan,AF,AF,AFG,4.00,Kabul,Asia,Afghani,AFA,26813057.00
AG,Antigua and Barbuda,AC,AG,ATG,28.00,Saint John's,Central America and the Caribbean,East Caribbean Dollar,XCD,66970.00
AI,Anguilla,AV,AI,AIA,660.00,The Valley,Central America and the Caribbean,East Caribbean Dollar,XCD,12132.00

我想存储每一行​​的第二个字段,这样我的数组只包含国家名称,如下所示:

string countryArray[] = {"Andorra,United Arab Emirates", "Afghanistan", "Antigua and Barbuda", "Anguilla"}

但是每次我运行我的代码时,都会发生段错误。这是我的代码:

countryArray[256];

if (myfile)
{
while (getline(myfile,line))
{
std::string s = line;
std::string delimiter = ",";

size_t pos = 0;
std::string token;
short loop=0;

while ((pos = s.find(delimiter)) != std::string::npos)
{
token = s.substr(0, pos);
s.erase(0, pos + delimiter.length());

if (loop < 2)
{
loop++;
}
else
{
loop+=11;
countryArray[count] = token;
count++;
}

}
}
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com