Reading text file EOF issue

I need to take values from a text file that looks like this:


1
2
3
4
Name		Account		        Date	     Outlets  Cable
John Public	01430-211230-28-9	02/02/2002	2	B
Walter Newman	29597-006105-26-2	08/16/2002	6	B
Charles Steger	18096-005492-41-6	04/14/2002	1	N


The issue is that I will not know the size of the text file, so I am currently using a while loop to stop reading the text when the EOF is reached. Here is the code I am currently using:

1
2
3
4
5
6
iFile.ignore(80, '\n');
while ( !iFile.eof() )
{
iFile >> FirstName >> SurName >> Account >> Date >> Outlets >> Cable;
oFile << FirstName << ' ' << SurName << Account << Date << endl;
}

The output produced from this includes the last line of text twice:

John Public  01430-211230-28-9  02/02/2002
Walter Newman 29597-006105-26-2  08/16/2002
Charles Steger 18096-005492-41-6  04/14/2002
Charles Steger 18096-005492-41-6  04/14/2002


What do I need to change within the loop to allow this to work?

Thanks for reading.
From ym understanding this is often the case in a while loop. Perhaps use a for and if set?

With the if statement to check the !eof() arguement and it then also writes to file. Since this will return false upon finding the eof there ought be no repeat in the outfile. Of course this requires some kind of foreknowledge of the number of lines in the data...

Perhaps have those with in the while(!eof() ) which might allow you to execute indefinitely and then only write when you have not encountered the eof.
The iFile stream doesn't fail until after the next attempt at a read, so that's why you're getting a duplicate.

1
2
3
4
5
6
7
while ( true )
{
     iFile >> FirstName >> SurName >> Account >> Date >> Outlets >> Cable;
     if(iFile.eof()) break; //put your test for eof() before the output

     oFile << FirstName << ' ' << SurName << Account << Date << endl;
}
Topic archived. No new replies allowed.