这个问题与Loading a file into a vector非常相似;但是,在这种情况下,我想将其加载到unsigned chars的向量中。
使用其他问题的代码,加载unsigned char矢量的最佳方法是什么?
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));
}
答案 0 :(得分:6)
将std::vector<char> vec;
更改为std::vector<unsigned char> vec;