title: How to read/write string type member of a struct using binary file in/out in c++?
body: <p>I have 2 c++ code: one is for write data into a binary file, another is for read that file.</p>
  
  <p><code>write.cpp</code> code is as below:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  const int NAME_SIZE = 51;
  struct Data
  {
      char name[NAME_SIZE];
      int age;
  };
  
  int main()
  {
      Data person;
      char again;
  
      fstream people("people.db", ios::out | ios::binary);
  
      do
      {
          cout &lt;&lt; "Enter the following data about a "&lt;&lt; "person:\n";
          cout &lt;&lt; "Name: ";
          cin.getline(person.name, NAME_SIZE);
          cout &lt;&lt; "Age: ";
          cin &gt;&gt; person.age;
          cin.ignore();
  
          people.write(reinterpret_cast&lt;char *&gt;(&amp;person),sizeof(person));
  
          cout &lt;&lt; "Do you want to enter another record? ";
          cin &gt;&gt; again;
          cin.ignore();
      } while (again == 'Y' || again == 'y');
      people.close();
  
  
  
      return 0;
  }
  </code></pre>
  
  <p><code>read.cpp</code> code is as below:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  const int NAME_SIZE = 51;
  struct Data
  {
      char name[NAME_SIZE];
      int age;
  };
  
  int main()
  {
      Data person;
      char again;
      fstream people;
  
      people.open("people.db", ios::in | ios::binary);
  
      if (!people)
      {
          cout &lt;&lt; "Error opening file. Program aborting.\n";
          return 0;
      }
  
      cout &lt;&lt; "Here are the people in the file:\n\n";
      people.read(reinterpret_cast&lt;char *&gt;(&amp;person),sizeof(person));
  
      while (!people.eof())
      {
          cout &lt;&lt; "Name: ";
          cout &lt;&lt; person.name &lt;&lt; endl;
          cout &lt;&lt; "Age: ";
          cout &lt;&lt; person.age &lt;&lt; endl;
  
          cout &lt;&lt; "\nPress the Enter key to see the next record.\n";
          cin.get(again);
          people.read(reinterpret_cast&lt;char *&gt;(&amp;person),sizeof(person));
      }
      cout &lt;&lt; "That's all the data in the file!\n";
      people.close();
      return 0;
  }
  </code></pre>
  
  <p>Above mentioned codes work fine. The problem arises when I use string type members in the structure:</p>
  
  <p>new <code>write.cpp</code>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  struct Data
  {
      string name;
      int age;
  };
  
  int main()
  {
      Data person;
      char again;
  
      fstream people("people.db", ios::out | ios::binary);
  
      do
      {
          cout &lt;&lt; "Enter the following data about a "&lt;&lt; "person:\n";
          cout &lt;&lt; "Name: ";
          cin&gt;&gt;person.name;
          cout &lt;&lt; "Age: ";
          cin &gt;&gt; person.age;
          cin.ignore();
  
          people.write(reinterpret_cast&lt;char *&gt;(&amp;person),sizeof(person));
  
          cout &lt;&lt; "Do you want to enter another record? ";
          cin &gt;&gt; again;
          cin.ignore();
      } while (again == 'Y' || again == 'y');
      people.close();
  
  
  
      return 0;
  }
  </code></pre>
  
  <p>new <code>read.cpp</code>:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  struct Data
  {
      string name;
      int age;
  };
  
  int main()
  {
      Data person;
      char again;
      fstream people;
  
      people.open("people.db", ios::in | ios::binary);
  
      if (!people)
      {
          cout &lt;&lt; "Error opening file. Program aborting.\n";
          return 0;
      }
  
      cout &lt;&lt; "Here are the people in the file:\n\n";
      people.read(reinterpret_cast&lt;char *&gt;(&amp;person),sizeof(person));
  
      while (!people.eof())
      {
          cout &lt;&lt; "Name: ";
          cout &lt;&lt; person.name &lt;&lt; endl;
          cout &lt;&lt; "Age: ";
          cout &lt;&lt; person.age &lt;&lt; endl;
  
          cout &lt;&lt; "\nPress the Enter key to see the next record.\n";
          cin.get(again);
          people.read(reinterpret_cast&lt;char *&gt;(&amp;person),sizeof(person));
       }
       cout &lt;&lt; "That's all the data in the file!\n";
       people.close();
      return 0;
  }
  </code></pre>
  
  <p>Now when I run <code>read.cpp</code> the program can't read string and the program crashes. I must use string as a member of the structure. How to solve this problem?</p>