2013-07-11 1 views
0

Visual Studio 2012 (32 비트)의 C++ 응용 프로그램에서 작업하고 있습니다. fstream을 사용하여 파일을 읽고 4 바이트를 두 번 읽으면 tellg에서 혼란스러운 값을 얻습니다. 0, 4 및 8을 기대하고있었습니다.C++ fstream tellg 동작

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

왜 이런 일이 발생합니까?

난 같는 fopen, fread를 사용하는 코드를 변경하고 ftell은 다음 POS의 값은 0, 4,

usedBlocks 8은 boost::int32이다이다. boost::int32은 실제로는 구조체가 아닌 int입니다. 그것들을 int로 변경해도 같은 결과가됩니다.

+0

예약 된 크기 유형은 무엇입니까? – Alexis

+2

나는'open' 함수의 오버로드에 익숙하지 않다. 나는 그것이 표준이 아니라고 확신한다. 어떤 경우이든, 그것은'ios :: binary'를 두는 곳이 아닙니다. 두 번째 매개 변수에서'ios :: in'과 비트 단위로 OR 연산을 수행해야합니다. 즉,'file.open (filename, std :: ios :: in std :: ios :: binary);'세 번째 매개 변수는 다른 것 (파일 보호 명세)을위한 것이며, 아마 그것을 사용하지 않아야한다. –

+0

고맙습니다. 거기에 쉼표를 실수로 넣었습니다. 나는 | – Haunted

답변

1

디버거에서 pos의 값을보고있는 경우 최적화로 인해 잘못되었을 수 있습니다.

pos 값을 표준 출력에 인쇄 해보십시오.

+0

디버거에서 값을 보았습니다. 그것이 틀릴 수 있다고 지적 해 주셔서 감사합니다. – Haunted