除了最后一句“Yury Chen 88 393 9202”之外,我还能读懂所有的字符串。
ifstream file("text.txt");
string s;
int counter = 0;
for(file >> s; !file.eof(); file >> s)
{
if (counter<=3){
templist.push_back(s);
counter +=1;
}
else{
list.push_back(templist);
counter = 0;
templist.clear();
templist.push_back(s);
counter +=1;
}
}
text.txt是
John Jones 12 5412354
Kevin Abatsa 23 6431264
Name Surname 31 1239523
Yury Chen 88 3939202
问题出在哪里?
问题可能在于:
for(file >> s; !file.eof(); file >> s)
当它流式传输文件中的最后一个字符串3939202
时,它会设置EOF
标志,因此您不处理该字符串就退出。 (我们不能确定,因为我们不能看到您的文件中3939202
后面是否有空格……)。
只需更改为:
while (file >> s)