while(infile) loop problem

Hey I'm having troubles with my while loop, Im sure it somethings simple, but basically, it loops one time too many for example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    ifstream file;
int line1, line2, line3;

    file.open("TriangleLines.txt");
while(file){

    GetNum(file,line1, line2, line3);
 cout<<line1<<" "<<line2<<" "<<line3;// this line gets outputed twice no matter where i place it.

cout<<endl;
  file.close();

}
    return 0;
}


so my problem is that line 10 gets outputed twice even where is only one line of data, or even if there are more then 2 lines, but it always reads the first line just fine.

Anyone knows whats going on?
what is GetNum? a Function? need that function.
or try this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
    ifstream file;
    string line;
    //int line1, line2, line3;

    file.open("TriangleLines.txt",ios::in);
    if(file.good())
	{
		while(!file.eof() && !line.empty())
		{
                       getline(file,line);
                       cout<<line;

                       cout<<endl;
                       file.clear();

                }

       }
        file.close();
        system("pause");
        return 0;
}
Last edited on
Topic archived. No new replies allowed.