<分区>
我必须读取一个文件,其中包含存储在 vector 中的路径列表。
vector<string> files;
ifstream in;
string x;
while( !in.eof() ) {
in >> x;
files.push_back(x);
}
但问题是,当读取最后一条路径时,in.eof() 仍然是 false 并且循环继续进行另一个不需要的步骤。 修复可能是这样的
vector<string> files;
ifstream in;
string x;
while( in >> x ) {
files.push_back(x);
}
但我认为对于 while 循环中更复杂的代码来说,这不是一个很好的解决方案。我错了吗?