How to use eof()?

Alright, I think I am doing this right but for some reason the outcome is not what's desired.

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
30
31
32
33
34
35
36
37
38
39
40
41
#include <fstream>
#include <cctype>
using namespace std;

int countInts(ifstream& infile);

int main() {
    ifstream infile;
    infile.open("3.txt", ios::in); //INPUT THE FIRST NUMBER
    
    if(!infile){
             cout << "Unalbe to open file";
             system("pause");
             }
    
    int result = countInts(infile); 
    if(result == -1){
              cout << "Data file read successfully" << endl;
              } else{
                     cout << "Bad Data in file." << endl;
                     }
    infile.close();                    //CLOSE THE STREAM
    system("pause");
    return 0;

}

int countInts(ifstream& infile){
    char dataIn; int sum = 0; int count = 0; int data; int length;
    while(infile >> dataIn){     //WHAT CONDITION IS NEEDED IN THE WHILE?
              if(!isdigit(dataIn)){
                    cout << "The number of integers read: " << count << endl;
                    return 0;
              }
              if(infile.eof()){ //I'm thinking error is here. I want it to display this if it is the end of file.
                    cout << "The number of integers read: " << count << endl;
                    return -1;
                    }
              count++;
    }
}


If my text file is: 12H3 the code works fine and it will display a count of 2 because there were two integers before it encountered a non-integer. However if I change the file to: 123 the code is giving me Bad Data in file. When really it should be giving me Data file read successfully as the program was able to reach the end of the file without encountering a non-integer. Any ideas?
Last edited on
Any ideas, or is that just not logical to have an if statement it we reach the end of a file?
I usually use a 'primary read' i.e. one before the loop starts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int countInts(ifstream& infile)
{
    char dataIn; int sum = 0; int count = 0; int data; int length;
    infile >> dataIn; // primary read
    while(!infile.eof())
    {     
              if(!isdigit(dataIn))
              {
                    cout << "The number of integers read: " << count << endl;
                    return 0;
              }
              count++;
              infile >> dataIn; // second read at end of loop
    }
    return -1;// will get here if we reach eof and havent bombed out
}


Don't do that. When using streams, read until it goes bad. Not all streams are files. The example aimmediately above should be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int countInts(istream& infile)
{
    char dataIn; int sum = 0; int count = 0; int data; int length;
    while (infile >> dataIn) // primary read
    {
              if (!isdigit(dataIn))
              {
                    cout << "The number of integers read: " << count << endl;
                    return 0;
              }
             ++count;
    }
    return -1;// will get here if we reach eof and havent bombed out
}
Thanks all!!
Last edited on
Topic archived. No new replies allowed.