title: C++ stream get unget not a nop?
body: <p>I have the following C++ program and ran it using Visual Studio 2008 on Windows 7. I get and then unget a character. After doing so, the file position is different. Why? How do I get around this problem?</p>
  
  <hr>
  
  <p>test.txt (download link below if you want)</p>
  
  <pre><code>/* Comment 1 */
  
  /* Comment 2 */
  </code></pre>
  
  <hr>
  
  <pre><code>#include &lt;fstream&gt;
  
  int main (int argc, char ** argv) {
      char const * file = "test.txt";
      std::fstream fs(file, std::ios::in);
      std::streampos const before = fs.tellg();
  
      // replacing the following two lines with
      // char c = fs.peek(); results in the same problem
      char const c = fs.get();
      fs.unget();
  
      std::streampos const after = fs.tellg();
      fs.seekg(after);
      char const c2 = fs.get();
      fs.close();
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <ul>
  <li><code>c: 47 '/'  char</code></li>
  <li><code>c2:    -1 'ÿ'  char</code></li>
  <li><code>before:    {_Myoff=0 _Fpos=0 _Mystate=0 }  std::fpos&lt;int&gt;</code></li>
  <li><code>after: {_Myoff=0 _Fpos=-3 _Mystate=0 } std::fpos&lt;int&gt;</code></li>
  </ul>
  
  <p>Adding <code>| std::fstream::binary</code> to the constructor seems to solve the problem. Perhaps it has to do with newlines in the file? If so, why does it affect code that doesn't even get close to reading a newline?</p>
  
  <p>Updated with a seeking to the after position and getting another character.</p>
  
  <p>It seems that saving via Notepad vs. Vim makes a difference. Saving via Notepad makes the stream work okay.</p>
  
  <p>I have uploaded the file to google docs if you want to dl it:</p>
  
  <p><a href="https://docs.google.com/leaf?id=0B8Ufd7Rk6dvHZmYyZjgwYmItMTI3MC00MDljLWJjYTctMWMxYWM0ODk1MTE2&amp;hl=en_US" rel="nofollow">https://docs.google.com/leaf?id=0B8Ufd7Rk6dvHZmYyZjgwYmItMTI3MC00MDljLWJjYTctMWMxYWM0ODk1MTE2&amp;hl=en_US</a></p>