0

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();
}
Marek R
  • 32,568
  • 6
  • 55
  • 140

1 Answers1

4

This is standard problem! Do not use this kind of loop: while (!file1.eof())

It doesn't work since your file ends with tab which is not read with last number and as a result loops iterates again more then it is needed. During last iteration tab is read, but reading of val fails and you see that as duplicated value.

Fix it like this:

while (file1 >> val) {
    cout << "i am val: " << val << endl;
    cout << "i am conta:" << conta << endl;
    conta = conta + 1;
}

It tries to read integer value and loop is exacted only when it is successful. There can be multiple reasons of stooping this loop: end of file reached, integer value not found, IO operation failure.

Offtopic: this int A[len]; is not allowed in standard C++. It works in most compilers since it is allowed in C. In C++ it is recommended to use std::array (since C++11) or std::vector in such cases.

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • I tried. The length of the array is 5, and using while (file1 >> val), instead of having 5, as final result, as it should be, i have 3 instead. When I was using (!file1.eof()), i was thinking that maybe it does a loop when it shouldn't. – throwawayinvisible Sep 17 '19 at 09:26
  • and again, I tried with while (file1 >> value), the count inside of the loop, says 2, when the loop is finished, is 3. So, I really am not understanding why it counts wrong. – throwawayinvisible Sep 17 '19 at 09:30
  • [Fix works for me](https://wandbox.org/permlink/fa1z8azL2iWPKoTF). Probably you missed that `file1 >> val` should be moved from loop body to loop condition :). I was sure it was obvious and I didn't wrote this explicitly. – Marek R Sep 17 '19 at 09:58
  • Yes, I had understood, but still it's not working. It messes up the count. – throwawayinvisible Sep 17 '19 at 10:28
  • What does it mean: `still it's not working`? Please use https://wandbox.org/ to show the issue. – Marek R Sep 17 '19 at 13:56