How to read a binary file into a vector of unsigned chars

后端 未结 4 1076
不思量自难忘°
不思量自难忘° 2020-11-27 12:34

Lately I\'ve been asked to write a function that reads the binary file into the std::vector where BYTE is an unsigned char

4条回答
  •  抹茶落季
    2020-11-27 13:05

    std::ifstream stream("mona-lisa.raw", std::ios::in | std::ios::binary);
    std::vector contents((std::istreambuf_iterator(stream)), std::istreambuf_iterator());
    
    for(auto i: contents) {
        int value = i;
        std::cout << "data: " << value << std::endl;
    }
    
    std::cout << "file size: " << contents.size() << std::endl;
    

提交回复
热议问题