EOF problems

I have a .txt file with a path to a file on each line. My code opens this file & loops through each file to retrieve information. Everything is fine, but it seems to be trying to go one line beyond the text, then throws an error. How do I get it to stop at the end of the last line. PLEASE SEE CODE:

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
int main(){
    string line;
    ifstream file ("./file_list.txt");
    int count=0;
    char * file_ptr;
    char file_name[46];
    enum variable{LATITUDE=0,LONGITUDE=1,RAIN=2,WIND=3,TEMP=4,RH=5};
    FireWeatherWRF latitude, longitude,rain_temp,rain,wind,temp,rh;
    if (file.is_open()){
        while (!file.eof()){
            getline (file,line);
            for (int i=0;i<46;i++){file_name[i]=line[i];}
            file_ptr=file_name;
            if (count==0){
                latitude.read_WRF(file_ptr,LATITUDE);
                longitude.read_WRF(file_ptr,LONGITUDE);
                rain.read_WRF(file_ptr,RAIN);
                wind.read_WRF(file_ptr,WIND);
                temp.read_WRF(file_ptr,TEMP);
                rh.read_WRF(file_ptr,RH);
                count++;               
            }
            else{
                rain_temp.read_WRF(file_ptr,RAIN);
                cout<<rain_temp.data[0][0]<<endl;
                for (int lat=0;lat<SN;lat++){
                    for (int lon=0;lon<WE;lon++){
                        rain.data[lat][lon]=rain.data[lat][lon]+rain_temp.data[lat][lon];
                    }
                }
                if (count==24){count=0;}
                else {count++;}
            }
        }
        cout<<"cum rain: "<<rain.data[0][0]<<endl;
        file.close();                        
    }
    return 0;
}


output:
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
0
0
0
0
0
0
0
0
0
0
0
0
0
0.0785536
1.15065
1.9852
2.01943
2.01943
2.01943
2.01943
2.01943
2.01943
2.01943
2.01943
// this is where it is trying to read a line beyond any text
0x7fff1c934de0 is not valid
could not get pointer to NcVar latitude requested
Lines 10 and 11 should be:
10
11
        while (getline (file,line)) {

Please, Don't loop on eof(). Just say "No!"

Hope this helps.
So I changed it & I still get the same error. Any other ideas?
I don't believe you. Post your current code.
alright... I got the error to go away- but now i am not reading the last line of text...
Now, it's working, but i didn't change anything more than the condition statement of the while as suggested by Duoas-
It is one of those weird things about C++.
Glad to have helped.
:-)
Topic archived. No new replies allowed.