File Input reads last line twice c++(urgent)

void CLAN::dispclan() // function 3 display function
{
clrscr();
ifstream fin("clan.dat",ios::binary);
line();
cout<<"\n\t\t\tLIST OF CLANS IN ALLIANCE";
line();
cout<<"\n\n CLAN ID\tCLAN NAME\t\TOTAL WARS\n";

while(!fin.eof())
{
fin.read((char*)this,sizeof(CLAN));
cout<<"\n "<<clanid<<"\t\t"<<clanname;
cout<<"\t\t"<<total_wars;
}

getch();
fin.close();
} // end of display function
Check after reading that fin.gcount() == sizeof(CLAN). The EOF flag gets set when the get pointer is at the EOF and you try to read more data, not the instant the get pointer reaches the EOF.
It's a bit unintuitive, yes, but that's the way it works.
i got the sol. its in bold


void CLAN::dispclan() // function 3 display function
{
clrscr();
ifstream fin("clan.dat",ios::binary);
line();
cout<<"\n\t\t\tLIST OF CLANS IN ALLIANCE";
line();
cout<<"\n\n CLAN ID\tCLAN NAME\t\TOTAL WARS\n";

fin.read((char*)this,sizeof(CLAN));
while(fin)
{ cout<<"\n "<<clanid<<"\t\t"<<clanname;
cout<<"\t\t"<<total_wars;
fin.read((char*)this,sizeof(CLAN));

}

getch();
fin.close();
} // end of display function
Topic archived. No new replies allowed.