oRUMOo
oRUMOo

Reputation: 163

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 code. Maybe theres a trick with flush or sync (but both I tryed...) or somewhat else...

Upvotes: 1

Views: 136

Answers (2)

oRUMOo
oRUMOo

Reputation: 163

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

Upvotes: 0

Steve Wellens
Steve Wellens

Reputation: 20620

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.

Upvotes: 1

Related Questions