title: Read binary file c++
body: <p>I'm trying to read an image into a char array. Here is my try:</p>
  <pre><code>ifstream file (&quot;htdocs/image.png&quot;, ios::in | ios::binary | ios::ate);
  ifstream::pos_type fileSize;
  char* fileContents;
  if(file.is_open())
  {
      fileSize = file.tellg();
      fileContents = new char[fileSize];
      file.seekg(0, ios::beg);
      if(!file.read(fileContents, fileSize))
      {
          cout &lt;&lt; &quot;fail to read&quot; &lt;&lt; endl;
      }
      file.close();
                  
      cout &lt;&lt; &quot;size: &quot; &lt;&lt; fileSize &lt;&lt; endl;
      cout &lt;&lt; &quot;sizeof: &quot; &lt;&lt; sizeof(fileContents) &lt;&lt; endl;
      cout &lt;&lt; &quot;length: &quot; &lt;&lt; strlen(fileContents) &lt;&lt; endl;
      cout &lt;&lt; &quot;random: &quot; &lt;&lt; fileContents[55] &lt;&lt; endl;
      cout &lt;&lt; fileContents &lt;&lt; endl;
  }
  </code></pre>
  <p>And this is the output:</p>
  <pre><code>size: 1944
  sizeof: 8
  length: 8
  random: ?
  ?PNG
  </code></pre>
  <p>Can anyone explain this to me? Is there an end-of-file char at position 8? This example was taken from <a href="http://www.cplusplus.com/doc/tutorial/files/" rel="noreferrer">cplusplus.com</a></p>
  <p>Running Mac OS X and compiling with XCode.</p>