可能重复:
Why is iostream::eof inside a loop condition considered wrong?C++ - 为什么cin.eof()读取最后一个字符两次?
我有很简单的问题...
#include<iostream>
using namespace std;
int main()
{
char x;
while(!cin.eof())
{
cin>>x;
cout<<"char: "<< x << endl;
}
return 0;
}
为什么当我尝试运行这段代码在Linux上:
./file_name < test_file.txt
结果是:
炭:一个
炭:乙
炭:C
炭:d
炭:d
当test_file.txt只是:
abcd
参见[为什么的iostream :: EOF算错了一个循环条件里面?](http://stackoverflow.com/questions/5605125/why-是-iostreameof-内,一个循环条件考虑的,是错误的) –
因为它总是错,错,错用'EOF()'都没有。现在有成千上万的重复,如果一个问题不是重复的,它仍然包含相同的错误... grumble grumble抱怨 –