I am making a very simple function, where I made an array of integers, I made the write in a file, and then I am reading from the same file and returning the number of values put on the file. Technically the value should be the same as the length of the array, however, the result is different. I have put some couts during the loop, but not understanding where the problem might be.
int len = 5;
int main()
{
int A[len];
for (int i = 0; i < len; i = i + 1) {
cout << "insert a value: " << endl;
cin >> A[i];
}
ofstream file;
file.open("trial.txt");
for (int i = 0; i < len; i = i + 1) {
file << A[i] << '\t';
}
file.close();
ifstream file1;
file1.open("trial.txt");
int val;
int conta = 0;
cout << "count before while" << conta << endl;
while (!file1.eof()) {
file1 >> val;
cout << "i am val: " << val << endl;
cout << "i am conta:" << conta << endl;
conta = conta + 1;
}
cout << "i am conta after while: " << conta << endl;
cout << "the value should be 5: " << conta; //instead it shows 6
file1.close();
}