reading files

This is a discussion on reading files within the C++ Programming forums, part of the General Programming Boards category; I am reading from a data file sequentially, when i get to the last record in the file it reads ...

reading files

I am reading from a data file sequentially, when i get to the last record in the file it reads it twice and outputs to my output file twice. I am using a while loop with a not end of file condtion, is there a place where i need to add an extra read or something??

You may be reading past the end of the file within your loop. It is often easier to test the actual read operation for the end of the file rather than the state of the file. If the read fails then you can quit immeadiately.

Otherwise you're reading the last record, eof isn't being set so the loop continues, and then you're reading again. Only when this next read is carried is eof then set, which means the remainder of you loop will be executed when you've already reached the end of the file.

You're reading something into report.accountnum twice each loop. Loose the second one, otherwise it'll make your file reading get out of sync resulting in a bad read (where something like a char is extracted when a int is expected) which will cause the stream to fail and force your loop to exit.