Connecting Tech Pros Worldwide Help | Site Map

Last record added twice when reading ifstream

  #1  
Old March 23rd, 2007, 01:45 PM
Kenneth
Guest
 
Posts: n/a
Ok, so logically this code seems to make sense but for some reason it
reads the last record twice before hitting eof. Is ifstream.eof()
implementation dependent?

void LoadFile(ifstream &File, List &List)
{
User lUser;
while(!File.eof())
{
File >lUser;
List.insert(lUser,List.nodeCount());
}
}

  #2  
Old March 23rd, 2007, 02:05 PM
Stefan Naewe
Guest
 
Posts: n/a

re: Last record added twice when reading ifstream


On 3/23/2007 1:42 PM, Kenneth wrote:
Quote:
Ok, so logically this code seems to make sense but for some reason it
reads the last record twice before hitting eof. Is ifstream.eof()
implementation dependent?
>
void LoadFile(ifstream &File, List &List)
{
User lUser;
while(!File.eof())
{
File >lUser;
List.insert(lUser,List.nodeCount());
}
}
>
Look Here:

http://www.gnomesane.net/code/doc/noteof/

S.
--
Stefan Naewe
stefan_DOT_naewe_AT_atlas_DOT_de
  #3  
Old March 23rd, 2007, 02:45 PM
Rolf Magnus
Guest
 
Posts: n/a

re: Last record added twice when reading ifstream


Kenneth wrote:
Quote:
Ok, so logically this code seems to make sense but for some reason it
reads the last record twice before hitting eof. Is ifstream.eof()
implementation dependent?
>
void LoadFile(ifstream &File, List &List)
{
User lUser;
while(!File.eof())
{
File >lUser;
List.insert(lUser,List.nodeCount());
}
}
This is an FAQ (I'd even say one of the most F AQs).
It's answered in the C++ FAQ lite at http://www.parashift.com/c++-faq-lite .

Closed Thread