getting a problem while using files

when i put the following code the problem i am getting is that it displays the last bit of information, present in the file, twice other than that the code works perfectly.
what i mean is that if in the file the following information is there:-
book name book no
something 212
whatever 103

then the output is
212       something
103       whatever
103       whatever


1
2
3
4
5
6
7
ifstream ls("test.txt");
   while(ls)
   {
       ls.read((char*) &lib1,size);
       cout<<"\n "<<lib1.bno<<"          "<<lib1.bname;
   }
   ls.close();


can anyone please tell me what is wrong in the code...
Try putting the call to read() inside the condition of the while loop.
you try with code:

ifstream ls("test.txt");
while(ls)
{
ls.read((char*) &lib1,size);
if(ls.eof()) break;
cout<<"\n "<<lib1.bno<<" "<<lib1.bname;
}
ls.close();
You try with code:

1
2
3
4
5
6
7
8
ifstream ls("test.txt");
while(ls)
{
ls.read((char*) &lib1,size);
if(ls.eof()) break;
cout<<"\n "<<lib1.bno<<" "<<lib1.bname;
}
ls.close();
thanks for the help it worked ..
Topic archived. No new replies allowed.