在主要方面:
ifstream file("text.txt");
string line;
while (file) {
file>>line;
cout<<line<<endl;
}
在text.txt中:
hello
goodbye
输出:
hello
goodbye
goodbye
为什么最后一行打印两次?
最佳答案
复制:第一次阅读“再见”时,您不知道到达文件末尾并转到下一个迭代。然后无法读取,将eof
位置1,但打印出line
的当前值,该值仍为“再见”。
关于c++ - 文件输入无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12756711/