This question is very similar to Loading a file into a vector; however, in this case, I want to load it into a vector of unsigned chars.
Using the code from the other question, what is the best way to load an unsigned char vector?
std::vector<char> vec; // Would like this to be std::vector<unsigned char> 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<std::size_t>(length));
file.read(&vec.front(), static_cast<std::size_t>(length));
}