<分区>
我有一个以下列格式存储数据的文件(这只是一个小示例):
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++;
}
}
}
}