title: How I loop through ifstream::get without getting EOF?
body: <p>In this small program, I'm just outputting 4 chars to the file and then retrieving them.As you can see in the output the while loop reads only 4 chars and tellp gets the right position but once outside tellp gets -1.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  int main()
  {
      ofstream out("E:\\test.txt");
      out &lt;&lt; "1234";
      out.close();
  
      ifstream in("E:\\test.txt");
  
      int i=0;
      char ch;
      while(in.get(ch)){
          cout &lt;&lt; "inside while loop ch="&lt;&lt; ch &lt;&lt;" and its position is " &lt;&lt; in.tellg()&lt;&lt; endl;
          i++;
      }
      cout &lt;&lt; "outside while loop ch="&lt;&lt; ch &lt;&lt;" and its position is " &lt;&lt; in.tellg()&lt;&lt; " and int =" &lt;&lt; i &lt;&lt;endl;
      return 0;
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>inside while loop ch=1 and its position is 1
  inside while loop ch=2 and its position is 2
  inside while loop ch=3 and its position is 3
  inside while loop ch=4 and its position is 4
  outside while loop ch=4 and its position is -1 and int =4
  </code></pre>
  
  <p>and if I use :</p>
  
  <pre><code>while(in.get(ch) &amp;&amp; in.peek() != EOF){
      cout &lt;&lt; "inside while loop ch="&lt;&lt; ch &lt;&lt;" and its position is " &lt;&lt; in.tellg()&lt;&lt; endl;
      i++;
  }
  </code></pre>
  
  <p>the output:</p>
  
  <pre><code>inside while loop ch=1 and its position is 1
  inside while loop ch=2 and its position is 2
  inside while loop ch=3 and its position is 3
  outside while loop ch=4 and its position is -1 and int =3
  </code></pre>
  
  <p>but for this usage I only get all chars except the last one. and why I'm still getting -1 here after which I can't "seekg" anymore?!!!!!!</p>
  
  <p>How I loop through ifstream::get and getting all chars without getting EOF?</p>