problem with eof()

Mar 19, 2010 at 9:28am
hello everyone,I am trying to read data from a text file,such as

1234567
1234567
1234567

but i found that when reading the text file, there is an extra loop counted by the eof loop, the result become:
1234567
1234567
12345677
an extra "7" appear, did i do something wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char c,seat[3][7];
    
    fstream file;
    int i=0,j=0;
    file.open("aaa.txt");
    while (!(file.eof()))
    {            
          file.get(c);
          seat[i][j]=c;
          cout<<seat[i][j];
          if (j==6)
          {
             j=0;
             i++;
          }
          else j++;
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}

Mar 19, 2010 at 9:50am
eof() does not return true until you have already tried and failed to read the last character of the file. So on the second to last loop, eof() is still not true because you haven't tried to read *past* the last character.
Topic archived. No new replies allowed.