C++ fstream tellg behaviour -


i working on c++ application in visual studio 2012 (32-bit). when read file using fstream , read 4 bytes twice, confusing values tellg. expecting 0, 4 , 8.

std::fstream file; file.open(filename, std::ios::in , std::ios::binary); if ( !file.is_open()) {     throw exception("error opening file reading"); } int pos = file.tellg();  // pos 0 boost::int32_t usedblocks; int size = sizeof (usedblocks); file.read(reinterpret_cast<char*>(&usedblocks),sizeof(usedblocks)); pos = file.tellg();      //pos 3588 //read reserved size file.read(reinterpret_cast<char*>(&reservedsize),sizeof(reservedsize)); pos = file.tellg();     //pos 3592 

why happen?

i have changed code use fopen, fread, , ftell, , pos values 0, 4 , 8.

usedblocks boost::int32. boost::int32 int, not struct. changing them int gives same result.

if looking @ values of pos in debugger, may wrong due optimization.

try printing values of pos standard output.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -