I want to read a binary file into a vector of bytes vector<int8_t>
. The function shouldn't be aware of the file's encoding / language / etc, I just want the raw bytes.
I read through the answers to this question and this excellent article, and since my files are not very large the recommended solution seems to be
std::ifstream in("file.txt", std::ios::binary);
auto ss = std::ostringstream{};
ss << in.rdbuf();
auto s = ss.str();
and then creating a vector from s
.
However I don't like the idea of reading my raw bytes through an ostringstream
which is locale-aware, especially that its default locale depends on your machine's settings.
Are my concerns legit? Will this solution produce different results on different machines with different global locales? Should I call ss.imbue(std::locale::classic());
first? How do I guarantee that the ostringstream
won't mess with the content? Or does opening the file itself in a binary mode guarantee that?