开发者

help needed with parsing binary file

I am trying to interact with an open-source program by writing a C++ application what can read-m开发者_JS百科odify-write a binary file format used by that program. The good thing is that the program is open source, the bad thing is that its binary files are not following any "standard", just its own way of reading and saving.

So this is how the program is saving a bool variable in the file:

memset(keyword, 0, 100);
sprintf(keyword, "drawbox");
fout.write((char*)keyword, strlen(keyword)+1);
fout.write((char*)&m_drawBox, sizeof(bool));

and this is how it is reading a bool:

else if (strcmp(keyword, "drawbox") == 0)
fin.read((char*)&m_drawBox, sizeof(bool));

This part works fine, I can parse it. I can parse single variables like integers, too.

My problem is with the part when it saves vectors or quaternions.

Saving vector position:

memset(keyword, 0, 100);
sprintf(keyword, "position");
fout.write((char*)keyword, strlen(keyword)+1);
fout.write((char*)&m_position, 3*sizeof(float));

Reading vector position:

fin.read((char*)&m_volMax, 3*sizeof(float));
else if (strcmp(keyword, "position") == 0)

So these were all snippets from the original source code. In my code, if I try to use the following, I have problems.

Here are the parts from my code:

struct Vec
{
    float x,y,z;
};

Vec m_position;

else if (strcmp(keyword, "position") == 0)
    fin.read((char*) &kf.m_position, 3*sizeof(float));

But this doesn't work. Here are the values I get from a sample file:

m_position x = -1.07374e+008
m_position y = -1.07374e+008
m_position z = -1.07374e+008

So somehow the values don't get split into x, y, z, they get repeated into the same x, y, z parts!

Can someone help me what is causing this? Actually what is this way of saving files? Isn't it a very strange idea to write the memory directly into a file using (*char)?

Here is the binary data corresponding to the 3*float vector m_position, in the original file.

45 01 67 44 9E 57 19 C3 48 14 09 45


m_position x = -1.07374e+008

If you look with the debugger's raw memory view at the value you'll see that the byte values of the float is 0xcccccccc. That's a special value, the MSVC compiler uses it to initialize variables in the Debug build.

In other words, your m_position variable never got assigned.

Lacking more info, there's at least one simple explanation for this: the fin.read() call didn't read anything. I have to assume that your code doesn't contain any error checking, it is a common omission.


Actualy I did binary file parsing few weeks ago and got one ugly bug with the same result as yours. And the problem was that when I write binary data to file I forgot to close the file. So reading data and parsing it back to ascii I got the same results as yours.

  • Hans Passant said that fin.read() does not read anything. So ....

I'm not saying that this is the case of yours, but checking does not hurts, does it ?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜