#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Person{
private :
string name;
int age;
public :
Person(string name, int age){
this->name = name;
this->age = age;
}
void show(){
cout << "Name : " + name + "\n" << "Age : " << age << \n####################\n";
}
~Person(){
cout << "object " + name + " deleted\n";
}
};
int main(){
ifstream file("./files/C63.bin", ios::binary | ios::in);
if (!file.is_open()){
cout << "Error opening the file..\n";
} else {
cout << "successfully opened the file..\nThe contents of the binary file are :\n";
Person *p;
while (file.good()){
file.read((char *)p, sizeof(Person));
cout << "hi\n";
p->show();
}
file.close();
}
return 0;
}
At the code line - "file.read((char *)p, sizeof(Person));", segmentation fault occurs. The binary file exists at the specified location with a few person objects. What could have gone wrong?