Problem:
You're opening and closing the input stream inside of your operator>>. So everytime it is executed it opens your file at the beginning, then reads to the end and closes the file again. At the next call it starts all over again.
Explaination:
The used input stream keeps track of it's current reading position. If you reinitialize the stream in every call, you reset the position in the file thus starting at the beginning again. If you're interested you can also check the position with std::ifstream::tellg().
Possible solution:
Prepare the input stream outside of operator>>, then just read one dataset per call. After all reading is done close the file on the outside.
Example:
Calling code:
#include<fstream>
// ... other code
std::ifstream fis;
Person pp, cc, dd;
fis.open("fis.txt", std::ios::in); // prepare input stream
fis >> pp;
fis >> cc;
fis >> dd;
fis.close(); // after reading is done: close input stream
operator>> code:
std::ifstream& operator>>(std::ifstream& fis, Person &p)
{
// only read if everything's allright (e.g. eof not reached)
if (fis.good())
{
// read to p ...
}
/*
* return input stream (whith it's position pointer
* right after the data just read, so you can start
* from there at your next call)
*/
return fis;
}
while (!fis.eof())