开发者

Why it reads the file correctly only at second time

I have a file which first 64 bytes are:

0x00:  01 00 00 10 00 00 00 20 00 00 FF 03 00 00 00 10  
0x10:  00 00 00 10 00 00 FF 03 00 00 00 10 00 00 FF 03  
0x20:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  
0x30:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 

When i'm reading the file (mode read and write) at position 26 for 4 bytes I get 0 and the next time (at position 30) i get correctly 4096.

The code is:

// read LastDirectoryBlockStartByte...
seekg(26);
char * pCUIBuffer = new char[4];
read(pCUIBuffer, 4);
const unsigned int x1 = gcount ();
const unsigned int LastDirectoryBlockStartByte = *(unsigned int *)pCUIBuffer;

// read LastDirectoryBlockNumberItems...
seekg(30);
read(pCUIBuffer, 4);
const unsigned int x2 = gcount ();
const unsigned int LastDirectoryBlockNumberItems = *(unsigned int *)pCUIBuffer;

With gcount() I checked the bytes are read - and this were correctly both times 4. I have no idea to debug it.

---------- EDIT ----------

When I use the following code (with some dummy before) it reads correctly:

char * pCUIBuffer = new char[4];
seekg(26);
read(pCUIBuffer, 4);
const unsigned int x1 = gcount ();
seekg(26);
read(pCUIBuffer, 4);
const unsigned int x2 = gcount ();
const unsigned int LastDirectoryBlockStartByte = *(unsigned int *)pCUIBuffer;

// read LastDirectoryBlockNumberItems...
seekg(30);
read(pCUIBuffer, 4);
const unsigned int x3 = gcount ();
const unsigned int LastDirectoryBlockNumberItems = *(unsigned int *)pCUIBuffer;

The difficulty is that the code stands at the begining in a methode. And the "false readed value" has obviously nothing to do with the listed co开发者_运维百科de. Maybe theres a trick with flush or sync (but both I tryed...) or somewhat else...


You are saying that pCUIBuffer contains a pointer:

*(unsigned int *)pCUIBuffer;

And then you go get whatever it's pointing at...in RAM. That could be anything.


Now I'm writing an answer, because my attempt to contact TonyK failes (I asked for writing an answer).

The perfect answer to my question was to enable exceptions by calling exceptions (eofbit | failbit | badbit).

Rumo

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜