title: How to read a binary file into a vector of unsigned chars
body: <p>Lately I've been asked to write a function that reads the binary file into the <code>std::vector&lt;BYTE&gt;</code> where <code>BYTE</code> is an <code>unsigned char</code>. Quite quickly I came with something like this:</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;vector&gt;
  typedef unsigned char BYTE;
  
  std::vector&lt;BYTE&gt; readFile(const char* filename)
  {
      // open the file:
      std::streampos fileSize;
      std::ifstream file(filename, std::ios::binary);
  
      // get its size:
      file.seekg(0, std::ios::end);
      fileSize = file.tellg();
      file.seekg(0, std::ios::beg);
  
      // read the data:
      std::vector&lt;BYTE&gt; fileData(fileSize);
      file.read((char*) &amp;fileData[0], fileSize);
      return fileData;
  }
  </code></pre>
  
  <p>which seems to be unnecessarily complicated and the explicit cast to <code>char*</code> that I was forced to use while calling <code>file.read</code> doesn't make me feel any better about it.  </p>
  
  <hr>
  
  <p>Another option is to use <a href="http://www.cplusplus.com/reference/iterator/istreambuf_iterator/"><code>std::istreambuf_iterator</code></a>:</p>
  
  <pre><code>std::vector&lt;BYTE&gt; readFile(const char* filename)
  {
      // open the file:
      std::ifstream file(filename, std::ios::binary);
  
      // read the data:
      return std::vector&lt;BYTE&gt;((std::istreambuf_iterator&lt;char&gt;(file)),
                                std::istreambuf_iterator&lt;char&gt;());
  }
  </code></pre>
  
  <p>which is pretty simple and short, but still I have to use the <code>std::istreambuf_iterator&lt;char&gt;</code> even when I'm reading into <code>std::vector&lt;unsigned char&gt;</code>.</p>
  
  <hr>
  
  <p>The last option that seems to be perfectly straightforward is to use <a href="http://en.cppreference.com/w/cpp/io/basic_ifstream"><code>std::basic_ifstream&lt;BYTE&gt;</code></a>, which kinda expresses it explicitly that <em>"I want an input file stream and I want to use it to read <code>BYTE</code>s"</em>:</p>
  
  <pre><code>std::vector&lt;BYTE&gt; readFile(const char* filename)
  {
      // open the file:
      std::basic_ifstream&lt;BYTE&gt; file(filename, std::ios::binary);
  
      // read the data:
      return std::vector&lt;BYTE&gt;((std::istreambuf_iterator&lt;BYTE&gt;(file)),
                                std::istreambuf_iterator&lt;BYTE&gt;());
  }
  </code></pre>
  
  <p>but I'm not sure whether <a href="http://en.cppreference.com/w/cpp/io/basic_ifstream"><code>basic_ifstream</code></a> is an appropriate choice in this case.</p>
  
  <p><em>What is the best way of reading a binary file into the <code>vector</code>?</em> I'd also like to know what's happening <em>"behind the scene"</em> and what are the possible problems I might encounter (apart from stream not being opened properly which might be avoided by simple <a href="http://www.cplusplus.com/reference/fstream/ifstream/is_open/"><code>is_open</code></a> check).</p>
  
  <p>Is there any good reason why one would prefer to use <a href="http://www.cplusplus.com/reference/iterator/istreambuf_iterator/"><code>std::istreambuf_iterator</code></a> here?<br>
  (the only advantage that I can see is simplicity)</p>