User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 361,550 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,061 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 2940 | Replies: 2
Reply
Join Date: Oct 2004
Posts: 1
Reputation: Erica is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Erica Erica is offline Offline
Newbie Poster

Help End of file function

  #1  
Oct 3rd, 2004
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!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 11
Reputation: ilvmdy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
ilvmdy ilvmdy is offline Offline
Newbie Poster

Re: End of file function

  #2  
Oct 4th, 2004
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.
Reply With Quote  
Join Date: Sep 2004
Posts: 5,838
Reputation: Narue is a splendid one to behold Narue is a splendid one to behold Narue is a splendid one to behold Narue is a splendid one to behold Narue is a splendid one to behold Narue is a splendid one to behold Narue is a splendid one to behold Narue is a splendid one to behold 
Rep Power: 24
Solved Threads: 384
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: End of file function

  #3  
Oct 4th, 2004
>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 ) ) {
  /* Read from the file and process */
}
Or this:
while ( !in.eof() ) {
  // Read from the file and process
}
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 ) {
  /* Process and print */
}
Or
while ( getline ( in, line ) ) {
  // Process and print
}
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.
Member of: Beautiful Code Club.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 1:50 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC