std::ifstream infile;
infile.open(fullfilename, std::ios::binary);
std::vector<unsigned char> byteVect;
if (!infile.fail()) {
infile.seekg(0, std::ios_base::end);
int flsz = infile.tellg();
LOG("sz=%d, infile.fail() returned %d", flsz, infile.fail());
infile.seekg(0, std::ios_base::beg);
while (!infile.eof()) {
unsigned char byte;
infile >> byte;
if (infile.fail()) break;
byteVect.push_back(byte);
}
infile.close();
LOG("Loaded %d bytes into buffer", byteVect.size());
I then log the buffer to logcat with my favourite home made library function. Lots of zeroes but it's early doors still.
The problem is that not all the bytes are read in this way. I find a missing byte in the middle of the stream and it's bye bye successfull deserialization. I know not all the bytes are read because sometimes (whenever it fails) the first log of flsz
is one more than the next log of byteVect.size()
. I know it happens in the middle because I am watching the hexdump of input and output (Game of Thrones it isn't).
I can't see what's wrong with my code but I'd previously just stuck to C style fopen
fread
fwrite
but thought it time to evolve. I'm sure you'll find a million holes in my looping algo but I'm learning. Thanks and stuff.