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

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

    I would have thought that the first method, using the size and using stream::read() would be the most efficient. The "cost" of casting to char * is most likely zero - casts of this kind simply tell the compiler that "Hey, I know you think this is a different type, but I really want this type here...", and does not add any extra instrucitons - if you wish to confirm this, try reading the file into a char array, and compare the actual assembler code. Aside from a little bit of extra work to figure out the address of the buffer inside the vector, there shouldn't be any difference.

    As always, the only way to tell for sure IN YOUR CASE what is the most efficient is to measure it. "Asking on the internet" is not proof.

提交回复
热议问题