Eri*_*rik 34
您可以使用它eof来测试它报告的确切条件 - 您是否尝试读取文件的结尾.您不能使用它来测试是否有更多的输入要读取,或者读取是否成功,这是更常见的测试.
错误:
while (!cin.eof()) {
cin >> foo;
}
Run Code Online (Sandbox Code Playgroud)
正确:
if (!(cin >> foo)) {
if (cin.eof()) {
cout << "read failed due to EOF\n";
} else {
cout << "read failed due to something other than EOF\n";
}
}
Run Code Online (Sandbox Code Playgroud)
Pup*_*ppy 12
您不应该使用它,因为如果由于其他原因输入失败,您可能会被搞砸.
while(!file.eof()) {
int i;
file >> i;
doSomething(i);
}
Run Code Online (Sandbox Code Playgroud)
如果文件内容为"WHAARRRRRRGARBL",上述循环会发生什么?它无限循环,因为它不是文件的结尾,但你仍然无法从中提取int.
| 归档时间: |
|
| 查看次数: |
19468 次 |
| 最近记录: |