title: C++, weird behavior while reading binary ifstream
body: <p>For my first question here,
  I'd like to talk about reading binary files in C++;
  I'm recoding an ID3 tag library.</p>
  
  <p>I'm parsing the header which is a binary file,
  the first 10bytes are as follow:</p>
  
  <pre><code>ID3    = 3 bytes = constant identifier
  0xXXXX = 2 bytes = version (MSB: major version, LSB: minor. eg: 0x0301 = v3.1)
  0xXX   = 1 byte  = some flags
  4*0xXX = 4 bytes = size
  </code></pre>
  
  <p>here's the piece of code to process that :</p>
  
  <pre><code>char          id[4];
  uint16_t      version;
  uint8_t       flags;
  uint32_t      size;
  std::ifstream _stream;
  
  _stream = std::ifstream(_filename, std::fstream::binary);
  
  _stream.read(id, 3);
  id[3] = 0;
  // process id
  _stream.read((char *)&amp;version, 2);
  // process version
  _stream.read((char *)&amp;flags, 1);
  // process flags
  _stream.read((char* )&amp;size, 4);
  // process flags
  _stream.close();
  </code></pre>
  
  <p>everything works fine except for version.
  lets say it's v3.0 (0x0300),
  the value set in version is 0x03, I would understand this behavior in text mode as it would consider 0x00 as end of string but here I'm reading in binary. And use numeric formats.</p>
  
  <p>Other strange thing, if I process it in 2 times I can make it work, eg :</p>
  
  <pre><code>uint16_t version = 0;
  char     buff;
  
   _stream.read(&amp;buff, 1);
  version = (buff &lt;&lt; 8);
   _stream.read(&amp;buff, 1);
  version |= buff;
  </code></pre>
  
  <p>In this case the value of version is 0x0300.</p>
  
  <p>Do you have any idea why the first method doesn't work properly?
  Am I doing something wrong ?</p>
  
  <p>Anyways, thanks for your help,</p>
  
  <p>Cheers !</p>