gpt4 book ai didi

c++ - 带有额外最终迭代的文件回显循环

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:06:26 25 4
gpt4 key购买 nike

当此代码完成时,为什么我会得到额外的迭代(打印额外的行)? 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;
}

最佳答案

尝试

while(infile>>text) cout << text << endl;

相反。

关于c++ - 带有额外最终迭代的文件回显循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1527969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com