title: Reading object using fstream in C++
body: <p>I'm trying to use <code>fstream</code> to write an object to a file and read it after but when I try to show the object read on the screen using cout the error message <code>Segmentation fault: 11</code> appears. Someone could help me with this code? Thanks in advance!</p>
  
  <pre><code>Produto *p1 = new Produto(1, "Refrigerante");
  
  cout &lt;&lt; "Produto p1 (pre serializacao): (" &lt;&lt; p1-&gt;codigo &lt;&lt; ") " &lt;&lt; p1-&gt;descricao &lt;&lt; endl;
  
  ofstream serializationWriter;
  serializationWriter.open("myobject.dat", ios::binary);
  serializationWriter.write((char *)&amp;p1, sizeof(p1));
  
  Produto *p2;
  
  ifstream serializationReader;
  serializationReader.open("myobject.dat", ios::binary);
  serializationReader.read((char *)&amp;p2, sizeof(p2));
  
  cout &lt;&lt; "Produto p2 (pos serializacao): (" &lt;&lt; p2-&gt;codigo &lt;&lt; ") " &lt;&lt; p2-&gt;descricao &lt;&lt; endl;
  </code></pre>