![]() |
| ||
| End of file function My program is reading the last line of data from an infile twice. When I execute the program, the last line of data is being displayed twice. Please Help! |
| ||
| Re: End of file function Hey Erica, Welcome to the forum. I not a big expert or anything but I do know that most of the people like to see the program code so they can tell you exactly what is wrong. Hopefully someone can help. |
| ||
| Re: End of file function >My program is reading the last line of data from an infile twice. I'll bet my next paycheck that your file processing loop looks like this: while ( !feof ( in ) ) {
Or this:while ( !in.eof() ) {
The problem with that is feof (or stream.eof ) only returns true after an attempt has been made to read from the file and end-of-file was encountered. So your loop will iterate once more than you want, and because the input request failed you will use the last successfully read line. The result is that the last line of the file is processed twice.The solution is to use the return value of your input function as the loop condition: while ( fgets ( line, sizeof line, in ) != NULL ) {
Orwhile ( getline ( in, line ) ) {
As an alternative, you can use an infinite loop and use feof or stream.eof in an if statement immediately after your input functions read from the file. That way you can break from the loop before processing the last line twice. |
| All times are GMT -4. The time now is 7:27 pm. |
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC