title: C++ skipping bytes when reading from a binary file
body: <p>I put together some C++ code to extract PNGs from certain files. It's not the most refined, but it seems to be skipping over certain null bytes.</p>
  <p>Here's the code:</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  int main() {
      ifstream source(&quot;source.win&quot;, ios::out | ios::binary);
      char current[] = { 0,0,0,0 };
      char header[] = { (char)137, 'P', 'N', 'G' };
      char footer[] = { 'I', 'E', 'N', 'D' };
      char c = 0;
      int n = 0;
      ofstream out(&quot;png0.png&quot;, ios::out | ios::binary);
      out.close();
      while (source &gt;&gt; c) {
          if (out.is_open()) {
              out &lt;&lt; c;
          }
          current[0] = current[1];
          current[1] = current[2];
          current[2] = current[3];
          current[3] = c;
          if (current[0] == (char)137 &amp;&amp;
              current[1] == 'P' &amp;&amp;
              current[2] == 'N' &amp;&amp;
              current[3] == 'G') {
              n++;
              out.open(&quot;png&quot; + to_string(n) + &quot;.png&quot;, 6);
              out &lt;&lt; (char)137 &lt;&lt; &quot;PNG&quot;;
          }
          if (current[0] == 'I' &amp;&amp;
              current[1] == 'E' &amp;&amp;
              current[2] == 'N' &amp;&amp;
              current[3] == 'D') {
              out.close();
          }
      }
  }
  </code></pre>
  <p>and here's the beginning of a file that it outputs, and the beginning of the actual file it should output:</p>
  <pre><code>89 50 4E 47 1A 00 00 00 49 48 44 52 00 00 01 00
  00 00 01 00 08 06 00 00 00 5C 72 A8 66 00 00 5F
  49 44 41 54 78 9C ED DD 6F 88 65 75 ...
  </code></pre>
  <p>vs.</p>
  <pre><code>89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52
  00 00 01 00 00 00 01 00 08 06 00 00 00 5C 72 A8
  66 00 00 0A 5F 49 44 41 54 78 9C ED DD 6F 88 65
  75 ...
  </code></pre>
  <p>Edit: I switched from &gt;&gt; to get() and from &lt;&lt; to put(), but <code>0A</code> is still being put as <code>0D 0A</code></p>