Connecting Tech Pros Worldwide Forums | Help | Site Map

Last record added twice when reading ifstream

Kenneth
Guest
 
Posts: n/a
#1: Mar 23 '07
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());
}
}


Stefan Naewe
Guest
 
Posts: n/a
#2: Mar 23 '07

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
Rolf Magnus
Guest
 
Posts: n/a
#3: Mar 23 '07

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