title: reading and appending a file using fstream in C++
body: <p>I am trying to read a file using fstream. Then when I reach the end I append the file and write some staff
  file</p>
  
  <pre><code>abc
  </code></pre>
  
  <p>I wrote sample code</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;fstream&gt;
  #include&lt;queue&gt;
  
  using namespace std;
  int main(int argc, char **argv){
      fstream *binf;
      binf=new fstream("t.txt", ios::out|ios::in|ios::app);
      cout&lt;&lt;"EOF"&lt;&lt;EOF&lt;&lt;endl;
      while(true){
          cout&lt;&lt;"before peek"&lt;&lt;endl;
          cout&lt;&lt;"binf-&gt;tellg:"&lt;&lt;binf-&gt;tellg()&lt;&lt;endl;
          cout&lt;&lt;"binf-&gt;tellp:"&lt;&lt;binf-&gt;tellp()&lt;&lt;endl;
          cout&lt;&lt;"binf-&gt;peek()::int=("&lt;&lt;(int)binf-&gt;peek()&lt;&lt;")::char=("&lt;&lt;(char)binf-&gt;peek()&lt;&lt;")"&lt;&lt;endl;
          cout&lt;&lt;"after peek"&lt;&lt;endl;
          cout&lt;&lt;"binf-&gt;tellg:"&lt;&lt;binf-&gt;tellg()&lt;&lt;endl;
          cout&lt;&lt;"binf-&gt;tellp:"&lt;&lt;binf-&gt;tellp()&lt;&lt;endl;
          char c;
          if(binf-&gt;peek()==EOF){
              cout&lt;&lt;"file is not good"&lt;&lt;endl;
              break;
          }
          binf-&gt;get(c);
      }
      cout&lt;&lt;"binf-&gt;tellg:"&lt;&lt;binf-&gt;tellg()&lt;&lt;endl;
      cout&lt;&lt;"binf-&gt;tellp:"&lt;&lt;binf-&gt;tellp()&lt;&lt;endl;
      binf-&gt;seekg(3);
      binf-&gt;seekp(3);
      cout&lt;&lt;"binf-&gt;tellg:"&lt;&lt;binf-&gt;tellg()&lt;&lt;endl;
      cout&lt;&lt;"binf-&gt;tellp:"&lt;&lt;binf-&gt;tellp()&lt;&lt;endl;
      binf-&gt;put('H');
      cout&lt;&lt;"binf-&gt;tellg:"&lt;&lt;binf-&gt;tellg()&lt;&lt;endl;
      cout&lt;&lt;"binf-&gt;tellp:"&lt;&lt;binf-&gt;tellp()&lt;&lt;endl;
      binf-&gt;seekg(4);
      char c;
      binf-&gt;get(c);
      cout&lt;&lt;"c:"&lt;&lt;c&lt;&lt;endl;
      binf-&gt;clear();
      binf-&gt;close();
      delete binf;
      return 0;
  }
  </code></pre>
  
  <p>This is the output I get from running this code</p>
  
  <pre><code>EOF-1
  before peek
  binf-&gt;tellg:0
  binf-&gt;tellp:0
  binf-&gt;peek()::int=(97)::char=(a)
  after peek
  binf-&gt;tellg:0
  binf-&gt;tellp:0
  before peek
  binf-&gt;tellg:1
  binf-&gt;tellp:1
  binf-&gt;peek()::int=(98)::char=(b)
  after peek
  binf-&gt;tellg:1
  binf-&gt;tellp:1
  before peek
  binf-&gt;tellg:2
  binf-&gt;tellp:2
  binf-&gt;peek()::int=(99)::char=(c)
  after peek
  binf-&gt;tellg:2
  binf-&gt;tellp:2
  before peek
  binf-&gt;tellg:3
  binf-&gt;tellp:3
  binf-&gt;peek()::int=(10)::char=(
  )
  after peek
  binf-&gt;tellg:3
  binf-&gt;tellp:3
  before peek
  binf-&gt;tellg:4
  binf-&gt;tellp:4
  binf-&gt;peek()::int=(-1)::char=(ÿ)
  after peek
  binf-&gt;tellg:-1
  binf-&gt;tellp:-1
  file is not good
  binf-&gt;tellg:-1
  binf-&gt;tellp:-1
  binf-&gt;tellg:-1
  binf-&gt;tellp:-1
  binf-&gt;tellg:-1
  binf-&gt;tellp:-1
  c:
  </code></pre>
  
  <p>All I am trying to do is to fix the file and write at the end
  however, whenever I see the EOF at the end of file the file is no longer good to use
  even with peek()</p>