I want to read N bytes of data from a file stream and add them to a vector. So let's say we have
basic_ifstream<uint8_t> myFileStream;
and a
vector<uint8_t> myBuffer;
I am currently doing something like this:
myBuffer.reserve(N); for (int i=0; i<N; ++i) { uint8_t tmpByte; myFileStream.read(&tmpByte, 1); myBuffer.push_back(tmpByte); }
but it is very slow.
Now I tried to let myFileStream.read copy the data directly to the vector. Since the vector stores its elements in adjacent storage, I thought something like this should be possible:
uint8_t* ptr = &myBuffer.back();
But with this, I get a runtime error (heap corruption). What is wrong with this decision? Or is there a better way to do this anyway?
source share