It displays alright. But then when I try to terminate the program it throws exception "Unhandled exception at 0x5036CCC8 (msvcp110d.dll) in VotingSys.exe: 0xC0000005: Access violation reading location 0x0045A2AC."
Line 41: You can't do a raw (binary) read of a std::string. A std::string contains an internal pointer to dynamically allocated memory. The value of the pointer when you wrote it out is going to be meaningless when you attampt to read back the base struct of a std::string. You're not going to get the memory the string object pointed to.
Since you didn't post the declaration of Person, it's impossible to tell if you also have the same problem in the base class. I'm guessing that you do, since you have references to name and age, which don't appear in Candidate.
You can accomplish what you want by:
while (file >> vote_received >> party)
This does not however read any base class members (if any). Again, Person is not shown.
BTW, what is the point of the loop at line 40? If you read more than one Candidate from the file, display() is only going to display the last one.
I made a suggestion above (at least for Candidate).
Assuming name and age are protected members of Person, you could do the following:
while (file >>name >> age >> vote_received >> party)
This assumes the above variables are written out in text mode, not binary mode.
However, this isn't the most desirable solution though, since Candidate now references members of Person. i.e. If you add another variable to Person, you must change Candidate.
A better solution is to provide a read function for each object: