title: How to read exactly one byte in a binary file in C++
body: <p>I am trying to a read a single byte from a binary file, and I am getting inaccurate results.</p>
  
  <p>This is the content of binary file:</p>
  
  <p><code>00000000  00 04 0A 07 00 00 00 00 00 00 74 00 00 00 00 61 69 6E 62 6F 77 00                                                                               ..........t....ainbow.
  </code></p>
  
  <p>The point is that I can read more than one byte, but I just can't read exactly one byte. If tried to read the third byte <code>0A</code> which is equal to <code>10</code>, instead it gives me a value of <code>32522</code> or in hexadecimal <code>7F0A</code>. What am I missing here?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;cstring&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  int main()
  {
      fstream file("foo.daf", ios::in | ios::out | ios::binary);
  
      file.seekg(2);
  
      int x;
  
      file.read(reinterpret_cast&lt;char *&gt;(&amp;x), 1);
  
      cout&lt;&lt;x&lt;&lt;endl;
  
      return 0;
  }
  </code></pre>