title: Loading a file into a vector<unsigned char>
body: <p>This question is very similar to <a href="https://stackoverflow.com/questions/7241871/loading-a-file-into-a-vectorchar">Loading a file into a vector</a>; however, in this case, I want to load it into a vector of unsigned chars.  </p>
  
  <p>Using the code from the other question, what is the best way to load an unsigned char vector?</p>
  
  <pre><code>std::vector&lt;char&gt; vec;  // Would like this to be std::vector&lt;unsigned char&gt; vec;
  std::ifstream file;
  file.exceptions(
      std::ifstream::badbit
    | std::ifstream::failbit
    | std::ifstream::eofbit);
  file.open("test.txt");
  file.seekg(0, std::ios::end);
  std::streampos length(file.tellg());
  if (length) {
      file.seekg(0, std::ios::beg);
      vec.resize(static_cast&lt;std::size_t&gt;(length));
      file.read(&amp;vec.front(), static_cast&lt;std::size_t&gt;(length));
  }
  </code></pre>