当这段代码完成时,为什么我会得到一个额外的迭代(打印额外的行)?EOF 是否需要额外的换行符?我宁愿不必添加额外/特殊字符来标记 EOF。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream infile("dictionary.txt"); // one word per line
string text;
while(infile){
infile >> text;
cout << text << endl;
}
infile.close();
return 0;
}