I'd like to read a number of bytes from an istream
into a vector
. I thought this would have the answer but it's actually asking an entirely different thing.
My code so far:
std::vector<char> read_buffer(std::istream& is, const std::size_t size) {
std::vector<char> buf;
buf.reserve(size);
is.read(buf.data(), size);
return buf;
}
This doesn't work because vector
was written into without it knowing, so after the is.read
, its size is still 0, not size
. What's the proper way to implement this function?