将文件加载到向量<unsigned char =“”> </unsigned>

时间:2012-04-17 14:28:47

标签: c++ iostream

这个问题与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));
}

1 个答案:

答案 0 :(得分:6)

std::vector<char> vec;更改为std::vector<unsigned char> vec;