I'm trying to read from a Person.txt file that I have already wrote a Person object to. I get this error: Exception thrown: read access violation. _Pnext was 0x87E504.
I've tried debugging and it executes the function just fine. It only pops the error when the function has been complete.
Here is my code for reading from the file:
void readFromFile()
{
ifstream inFile("Person.txt", ios::in);
Person p2;
inFile.read(reinterpret_cast<char*>(&p2), sizeof(Person));
cout << "First Name: " << p2.getFirst() << endl
<< "Last Name: " << p2.getlast() << endl
<< "Gender: " << p2.getgender() << endl
<< "Age: " << p2.getAge() << endl;
}
Person class:
Class Person {
public:
Person()
{
firstName = "N/A";
lastName = "N/A";
gender = "N/A";
age = 0;
}
Person(std::string first, std::string last, std::string gender, int age)
{
firstName = first;
lastName = last;
this->gender = gender;
this->age = age;
}
~Person()
{
std::cout << "Person Destructor" << std::endl;
}
void setFirst(std::string first)
{
firstName = first;
}
std::string getFirst() const
{
return firstName;
}
void setLast(std::string last)
{
lastName = last;
}
std::string getlast() const
{
return lastName;
}
void setGender(std::string gender)
{
this->gender = gender;
}
std::string getgender() const
{
return gender;
}
void setAge(int age)
{
this-> age = age;
}
int getAge() const
{
return age;
}
private:
std::string firstName;
std::string lastName;
std::string gender;
int age;
};