难以阅读由空格C ++分隔的行

时间:2016-10-21 18:30:03

标签: c++

我需要在C ++中读取我的模拟文件,文件必须采用以下格式:

0,   0
200, 0
200, 100
400, 100
400, 200
0,   200

这是我第一次处理文件,但我正在阅读并尝试这样做:

# Comments
# Comments
nameOfTheMarket
timeOfSimulation
averageTime
numberOfCashiers
cashierName1 quality1 salary1
cashierName2 quality2 salary2
    -          -       -
cashierNameN qualityN salaryN
#End

我的困难在于阅读收银员的姓名,质量和薪水。我真的需要使用“while(info [i]!='')”吗?那不行。

1 个答案:

答案 0 :(得分:0)

而不是info.length()在循环中使用stoi(numberOfCashiers)来使用getline读取下一行

当您分割字符串时,您可以通过多种方式执行此操作,例如。

std::istringstream is(input);

std::string cashiername;
int quality=0;  // not sure about the data type u need.
int salary=0;

is >> cashiername >> quality >> salary