C++ ifstream of image file
<p>I am having trouble reading image file into a buffer. When I read normal ascii file, everything is fine and dandy, but when it comes to image file I suspect there is a \0 character inside an image file?</p>

<p>I needed to take in image file, parsed it into 16KB blocks for hashing, but when I do</p>

<pre><code> std::ifstream ifs;
 ifs.open(file_name, std::ifstream::binary | std::ifstream::in);
 .     
 .
 .
 std::string block;
 char buffer[BLOCK_SIZE];
 .
 .
 .
 memset(buffer, 0, BLOCK_SIZE);
 ifs.read(buffer, BLOCK_SIZE);
 block = buffer;

 std::cout &lt;&lt; i &lt;&lt; " | block size: " &lt;&lt; block.length() 
           &lt;&lt; " | buffer size: " &lt;&lt; strlen(buffer) &lt;&lt; std::endl;

 hash = sha256(block); // string type required for openssl's function
</code></pre>

<p>I get</p>

<pre><code>0 | block size: 4 | buffer size: 4
1 | block size: 16 | buffer size: 16
2 | block size: 88 | buffer size: 88
3 | block size: 57 | buffer size: 57
4 | block size: 109 | buffer size: 109
5 | block size: 26 | buffer size: 26
6 | block size: 65 | buffer size: 65
</code></pre>

<p>How do I resolve this issue? I am suspecting, for example</p>

<pre><code>junkjunkjunk\0junkjunkjunk
</code></pre>

<p>that the image file have random \0</p>