我有以下代码:
ifstream f("x.txt");
string line;
while (f.good()) {
getline(f, line);
// Use line here.
}
Run Code Online (Sandbox Code Playgroud)
但这会两次读到最后一行.为什么会发生这种情况,我该如何解决?
与以下情况非常相似:
ifstream f("x.txt");
string line;
while (!f.eof()) {
getline(f, line);
// Use line here.
}
Run Code Online (Sandbox Code Playgroud)