title: The read method returns the last object twice from the file
body: <p>The read method returns the last object twice. I tried using tellg before read line and it seems as if the last object itself is written twice in file.</p>
  <pre class="lang-cpp prettyprint-override"><code>#include&lt;iostream&gt;
  #include&lt;fstream&gt;
  #include&lt;string&gt;
  using namespace std;
  class Abc
  {
      string uname, pword;
  public:
      void getdetails()
      {
          cout &lt;&lt; &quot;enter a password&quot;;
          getline(cin, pword);
          cout &lt;&lt; &quot;enter username&quot;;
          getline(cin, uname);
      }
      void printdetails()
      {
          cout &lt;&lt; uname &lt;&lt; &quot; &quot; &lt;&lt; pword;
      }
  };
  int main()
  {
      fstream f;
      int i, j;
      f.open(&quot;gamma.txt&quot;, ios::out | ios::binary | ios::app);
      Abc a;
      for (i = 0; i &lt; 2; i++)
      {
          a.getdetails();
          f.write((char*) &amp;a, sizeof(a));
      }
      f.close();
      f.open(&quot;gamma.txt&quot;, ios::in | ios::binary);
      for (i = 0; i &lt; 2; i++)
      {
          f.read((char*) &amp;a, sizeof(a));
          a.printdetails();
      }
      f.close();
  }
  </code></pre>
  <p>This is the output. Instead of getting both the objects I'm getting the second one twice.</p>
  <pre><code>enter a password
  123
  enter username
  ralph
  enter a password
  321
  enter username
  john
  john 321john 321
  </code></pre>