title: Loading and Saving WAV file
body: <p>I'm trying to load a WAVE file into a data structure, then save it back to disk as a copy of the original. Loading and saving appears to work fine but the copied WAVE file won't play any sound (although It does open up without errors). I'm wondering if this has anything to do with endian-ness?</p>
  
  <p>This is my data structure to hold the WAVE file:</p>
  
  <pre><code>struct WaveFile
  {
  public:
  
      static const uint16 NUM_CHARS = 4;
  
  public:
  
      WaveFile() : Data(nullptr) {}
  
      ~WaveFile() { delete[] Data; }
  
      char ChunkID[NUM_CHARS];
  
      uint32 ChunkSize;
  
      char Format[NUM_CHARS];
  
      char SubChunkID[NUM_CHARS];
  
      uint32 SubChunkSize;
  
      uint16 AudioFormat;
  
      uint16 NumChannels;
  
      uint32 SampleRate;
  
      uint32 ByteRate;
  
      uint16 BlockAlign;
  
      uint16 BitsPerSample;
  
      char SubChunk2ID[NUM_CHARS];
  
      uint32 SubChunk2Size;
  
      byte* Data; 
  };
  </code></pre>
  
  <p>This is how I load it in:</p>
  
  <pre><code>std::ifstream file(filename, std::ios::binary);
  
  if (file.good())
  {
      file.read(waveFile.ChunkID, WaveFile::NUM_CHARS);
      file.read(reinterpret_cast&lt;char*&gt;(&amp;waveFile.ChunkSize), size_ui32);
      file.read(waveFile.Format, WaveFile::NUM_CHARS);
      file.read(waveFile.SubChunkID, WaveFile::NUM_CHARS);
      file.read(reinterpret_cast&lt;char*&gt;(&amp;waveFile.SubChunkSize), size_ui32);
      file.read(reinterpret_cast&lt;char*&gt;(&amp;waveFile.AudioFormat), size_ui16);
      file.read(reinterpret_cast&lt;char*&gt;(&amp;waveFile.NumChannels), size_ui16);
      file.read(reinterpret_cast&lt;char*&gt;(&amp;waveFile.SampleRate), size_ui32);
      file.read(reinterpret_cast&lt;char*&gt;(&amp;waveFile.ByteRate), size_ui32);
      file.read(reinterpret_cast&lt;char*&gt;(&amp;waveFile.BlockAlign), size_ui16);
      file.read(reinterpret_cast&lt;char*&gt;(&amp;waveFile.BitsPerSample), size_ui16);
      file.read(waveFile.SubChunk2ID, WaveFile::NUM_CHARS);
      file.read(reinterpret_cast&lt;char*&gt;(&amp;waveFile.SubChunk2Size), size_ui32);
      waveFile.Data = new byte[waveFile.SubChunk2Size];
      file.read(reinterpret_cast&lt;char*&gt;(waveFile.Data), sizeof(waveFile.SubChunk2Size));
      file.close();
  }
  </code></pre>
  
  <p>And this is how I write the data back out to a file:</p>
  
  <pre><code>std::ofstream file(outfile, std::ios::binary);
  
  if (file.good())
  {
      file.flush();
      file.write(waveFile.ChunkID, WaveFile::NUM_CHARS);
      file.write(reinterpret_cast&lt;const char*&gt;(&amp;waveFile.ChunkSize), size_ui32);
      file.write(waveFile.Format, WaveFile::NUM_CHARS);
      file.write(waveFile.SubChunkID, WaveFile::NUM_CHARS);
      file.write(reinterpret_cast&lt;const char*&gt;(&amp;waveFile.SubChunkSize), size_ui32);
      file.write(reinterpret_cast&lt;const char*&gt;(&amp;waveFile.AudioFormat), size_ui16);
      file.write(reinterpret_cast&lt;const char*&gt;(&amp;waveFile.NumChannels), size_ui16);
      file.write(reinterpret_cast&lt;const char*&gt;(&amp;waveFile.SampleRate), size_ui32);
      file.write(reinterpret_cast&lt;const char*&gt;(&amp;waveFile.ByteRate), size_ui32);
      file.write(reinterpret_cast&lt;const char*&gt;(&amp;waveFile.BlockAlign), size_ui16);
      file.write(reinterpret_cast&lt;const char*&gt;(&amp;waveFile.BitsPerSample), size_ui16);
      file.write(waveFile.SubChunk2ID, WaveFile::NUM_CHARS);
      file.write(reinterpret_cast&lt;const char*&gt;(&amp;waveFile.SubChunk2Size), size_ui32);
      file.write(reinterpret_cast&lt;const char*&gt;(waveFile.Data), waveFile.SubChunk2Size);
      file.close();   
  }
  </code></pre>
  
  <p>The size of the copy is the same as the original also. If anyone is wondering, uint16, uint32 and byte are just typedefs for unsigned short, unsigned int and unsigned char. size_ui32 variables are just this:</p>
  
  <pre><code>static const uint32 size_ui32 = sizeof(uint32);
  static const uint32 size_ui16 = sizeof(uint16);
  </code></pre>
  
  <p>That't about it, can't seem to find out where I've gone wrong.</p>
  
  <p>Cheers for any help.</p>