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

后端 未结 4 1082
不思量自难忘°
不思量自难忘° 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:14

    When testing for performance, I would include a test case for:

    std::vector readFile(const char* filename)
    {
        // open the file:
        std::ifstream file(filename, std::ios::binary);
    
        // Stop eating new lines in binary mode!!!
        file.unsetf(std::ios::skipws);
    
        // get its size:
        std::streampos fileSize;
    
        file.seekg(0, std::ios::end);
        fileSize = file.tellg();
        file.seekg(0, std::ios::beg);
    
        // reserve capacity
        std::vector vec;
        vec.reserve(fileSize);
    
        // read the data:
        vec.insert(vec.begin(),
                   std::istream_iterator(file),
                   std::istream_iterator());
    
        return vec;
    }
    

    My thinking is that the constructor of Method 1 touches the elements in the vector, and then the read touches each element again.

    Method 2 and Method 3 look most promising, but could suffer one or more resize's. Hence the reason to reserve before reading or inserting.

    I would also test with std::copy:

    ...
    std::vector vec;
    vec.reserve(fileSize);
    
    std::copy(std::istream_iterator(file),
              std::istream_iterator(),
              std::back_inserter(vec));
    

    In the end, I think the best solution will avoid operator >> from istream_iterator (and all the overhead and goodness from operator >> trying to interpret binary data). But I don't know what to use that allows you to directly copy the data into the vector.

    Finally, my testing with binary data is showing ios::binary is not being honored. Hence the reason for noskipws from .

提交回复
热议问题