title: Why do I get garbage when I try to parse the binary file in C++?
body: <p>I'm trying to parse .wav files in C++.<br />
  The 44 bytes in the header of the .wav file are some of the file's meta information, which I am trying to parse.<br />
  I parsed it in Python and got the following, which should be correct</p>
  <pre><code>Chunk_id  :  RIFF
  Chunk_size  :  468556
  Format  :  WAVE
  fmt_id  :  fmt 
  fmt_size  :  16
  audio_format  :  1
  channels_count  :  1
  sample_rate  :  44100
  byte_rate  :  88200
  block_align  :  2
  bits_per_sample  :  16
  data_id  :  data
  data_size  :  468520
  </code></pre>
  <p>But when I switch in to C++, I got this:</p>
  <pre><code>ChunkID: RIFFL&amp;
  ChunkSize: 468556
  Format: WAVEfmt 
  FmtID: fmt 
  FmtChunkSize: 16
  FmtAudioFormat: 1
  FmtChannelNumber: 1
  FmtSampleRate: 44100
  FmtByteRate: 88200
  FmtBlockAlign: 2
  FmtBitPerSample: 16
  DataChunkID: data(&amp;
  </code></pre>
  <p>The problem is three fields consisting of a char array of four bytes.</p>
  <p><code>ChunkID: RIFFL&amp;</code>, <code>Format: WAVEfmt</code>, <code>DataChunkID: data(&amp;</code><br />
  As parsed by Python, the contents of the three fields should be <code>RIFF</code>, <code>WAVE</code>, <code>data</code>.
  And this is my C++ code.</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  using namespace std;
  struct WaveChunk
  {
          char ChunkID[4];
          int ChunkSize;
          char Format[4];
  
          // fmt
          char FmtID[4];
          int FmtChunkSize;
          short FmtAudioFormat;
          short FmtChannelNumber;
  
          int FmtSampleRate;
          int FmtByteRate;
          short FmtBlockAlign;
          short FmtBitPerSample;
          // fmt
  
          // data
          char DataChunkID[4];
          int DataChunkSize;
          // data
  };
  
  string WaveChunkToString(WaveChunk* wavechunk){
      stringstream ss;
      ss &lt;&lt; &quot;ChunkID: &quot; &lt;&lt; wavechunk-&gt;ChunkID &lt;&lt; &quot;\n&quot;;
      ss &lt;&lt; &quot;ChunkSize: &quot; &lt;&lt; wavechunk-&gt;ChunkSize &lt;&lt; &quot;\n&quot;;
      ss &lt;&lt; &quot;Format: &quot; &lt;&lt; wavechunk-&gt;Format &lt;&lt; &quot;\n&quot;;
  
      ss &lt;&lt; &quot;FmtID: &quot; &lt;&lt; wavechunk-&gt;FmtID &lt;&lt; &quot;\n&quot;;
      ss &lt;&lt; &quot;FmtChunkSize: &quot; &lt;&lt; wavechunk-&gt;FmtChunkSize &lt;&lt; &quot;\n&quot;;
      ss &lt;&lt; &quot;FmtAudioFormat: &quot; &lt;&lt; wavechunk-&gt;FmtAudioFormat &lt;&lt; &quot;\n&quot;;
      ss &lt;&lt; &quot;FmtChannelNumber: &quot; &lt;&lt; wavechunk-&gt;FmtChannelNumber &lt;&lt; &quot;\n&quot;;
      ss &lt;&lt; &quot;FmtSampleRate: &quot; &lt;&lt; wavechunk-&gt;FmtSampleRate &lt;&lt; &quot;\n&quot;;
      ss &lt;&lt; &quot;FmtByteRate: &quot; &lt;&lt; wavechunk-&gt;FmtByteRate &lt;&lt; &quot;\n&quot;;
      ss &lt;&lt; &quot;FmtBlockAlign: &quot; &lt;&lt; wavechunk-&gt;FmtBlockAlign &lt;&lt; &quot;\n&quot;;
      ss &lt;&lt; &quot;FmtBitPerSample: &quot; &lt;&lt; wavechunk-&gt;FmtBitPerSample &lt;&lt; &quot;\n&quot;;
      ss &lt;&lt; &quot;DataChunkID: &quot; &lt;&lt; wavechunk-&gt;DataChunkID &lt;&lt; &quot;\n&quot;;
      ss &lt;&lt; &quot;DataChunkSize: &quot; &lt;&lt; wavechunk-&gt;DataChunkSize &lt;&lt; endl;
      return ss.str();
  }
  
  using namespace std;
  int main(){
      WaveChunk w;
      ifstream inf(&quot;target.wav&quot;, ios::binary|ios::in);
      inf.read((char* ) &amp;w, sizeof(WaveChunk));
      cout &lt;&lt; WaveChunkToString(&amp;w);
      return 0;
  }
  </code></pre>
  <p>That's it, why are these three fields parsed differently than expected, but the other fields made up of numbers are fine?</p>