title: write and read string to binary file C++
body: <p>Im having problems writing string into a binary file. This is my code:</p>
  
  <pre><code>ofstream outfile("myfile.txt", ofstream::binary);
  std::string text = "Text";
  outfile.write((char*) &amp;text, sizeof (string));
  outfile.close();
  </code></pre>
  
  <p>Then, I try to read it,</p>
  
  <pre><code>char* buffer = (char*) malloc(sizeof(string));
  ifstream infile("myfile.txt", ifstream::binary);    
  infile.read(buffer, sizeof (prueba));
  std::string* elem = (string*) buffer;
  cout &lt;&lt; *elem;
  infile.close();
  </code></pre>
  
  <p>I just cant get it to work. I am sorry, I am just desperate. Thank you!</p>