title: ifstream / ofstream issue with c++?
body: <p>I have been having a very hard time writing to a binary file and reading back. I am basically writing records of this format</p>
  
  <pre><code>1234|ABCD|efgh|IJKL|ABC
  </code></pre>
  
  <p>Before writing this record, I would write the length of this entire record ( <code>using string.size())</code> and then I write the record to the binary file using <code>ofstream</code> as follows: </p>
  
  <p>int size;</p>
  
  <pre><code>ofstream studentfile;
  studentfile.open( filename.c_str(),ios::out|ios::binary );
  studentfile.write((char*)&amp;size,sizeof(int));
       studentfile.write(data.c_str(),(data.size()*(sizeof(char))));
       cout &lt;&lt; "Added " &lt;&lt; data &lt;&lt; " to " &lt;&lt; filename &lt;&lt; endl;
       studentfile.close();
  </code></pre>
  
  <p>And I read this data at some other place</p>
  
  <pre><code>ifstream ifile11;
       int x;
       std::string y;
       ifile11.open("student.db", ios::in |ios::binary);
       ifile11.read((char*)&amp;x,sizeof(int));
       ifile11.read((char*)&amp;y,x);
       cout &lt;&lt; "X " &lt;&lt; x &lt;&lt; " Y " &lt;&lt; y &lt;&lt; endl;
  </code></pre>
  
  <p>first I read the length of the record into the variable x, and then read the record into string y. The problem is, the output shows x as being '0' and 'y' is empty.</p>
  
  <p>I am not able figure this out. Someone who can look into this problem and provide some insight will be thanked very much.</p>
  
  <p>Thank you</p>