私は少数しかないファイルから読む必要があります。それを読んで合計してください。 (ラーニングC++)私はfstreamの:: EOF()関数を使用していた場合、それは二回、最後の番号を読んで、私はこのようにそれをやった:while(file >> a)はなぜ機能しますか?
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream file;
int a = 0, sum = 0;
file.open("nums.txt",fstream::in);
if (!file.is_open()) {
cout << "Unable to open file!\n" << endl;
}
while (file >> a) {
cout << a << endl;
sum += a;
}
cout << "Sum: " << sum << endl;
file.close();
getchar();
return 0;
}
今では二回最後の番号を読み取ることはありませんが、私が何であるかを理解していませんwhile()内で起こっている、私は0を読めばそれは止まるだろうと思ったが、それも読む。ファイル>>は実際には "管理されている" = 1と "失敗" = 0のようなものを返していますか?
おかげ
[whileループで "std :: cin >> value"はどのように評価されますか?](https://stackoverflow.com/questions/34395801/how-is-stdcinvalue-evaluated-in-a- whileループ) –