Advertisement

Writing a binary file

Started by September 14, 2000 12:38 PM
2 comments, last by HJC 24 years, 3 months ago
Hello, I have a fairly big tree data structure for my game. I want to save this tree on to a binary file. I know how to save the contents of each nodes, but I don''t know what to do with the links between the nodes because they are pointers. Can anyone recommend a solution? Thanks.
I''m sure someone has a better idea, since I''ve never done this thing before... hey... actually, I''ll phrase this as a questiony thingy, since I''d also like to know the answer... um, anyway.

Would this work? ::
Giving each node a unique ID, and the IDs of the other nodes it links to. Then, when you write them to file, include that information. When your read it back, load them into an array or LList temporarily, then go through and link them all back up based on IDs... Hows that sound? Workable? Is there a better way?
Advertisement
quote: Original post by A. Buza

I''m sure someone has a better idea, since I''ve never done this thing before... hey... actually, I''ll phrase this as a questiony thingy, since I''d also like to know the answer... um, anyway.

Would this work? ::
Giving each node a unique ID, and the IDs of the other nodes it links to. Then, when you write them to file, include that information. When your read it back, load them into an array or LList temporarily, then go through and link them all back up based on IDs... Hows that sound? Workable? Is there a better way?


That''s possible... you could also write the TYPEID (check out the Windows GUI Programming in DirectDraw tutorials on this, or check out the Windows Help).

I''m developing a game datafile system similar to what''s done in POL (a UO shard server application)... datafiles are stored as follows:

Datatype Descriptor{    Variable1   Value1;    Variable2   Value2;} 


and so on. I''m also developing a class that will recursively read structures (like above) that are embedded inside other structures, so that they become lists unto themselves, like so:

Datatype Descriptor{    Variable1   Value1;    Variable2   Value2;    Datatype2 Descriptor2    {         Variable 3;    }} 


Hope that helps



MatrixCubed
I won''t write a precise example of how you do this because it will be a bit lengthy for the messageboard, but the basic idea is as follows.

Your basic node structure must contain a list of pointers to more node structures (obviously), a numeric ID, and a pointer to the actual data and its length in bytes. Also, stored in there somewhere must be how many child nodes it has.

struct node {
unsigned long ID;
unsigned long datalen;
void * data;
int childcount; // maximum of 5 or something
node * child[5];
};

okay, write a function to dump the contents of a single given node in this order:

ID, datalen, the contents of data and then childcount.

prototype it something like: void DumpNode(node * anode);

now the clever bit; At the bottom of the function, add the following:

for(int i = 0; i < anode->childcount; i++)
DumpNode(anode->child);

This will then call DumpNode recursively, ensuring that each child node is written, and each child child node and so on. When it has finished totally, the contents of your file will look something like this:

ID // 4 bytes
datalen // 4 bytes
data // datalen number of bytes
childcount // 4 bytes
child 1 ID // 4 bytes
child 1 datalen // 4 bytes
child 1 data // child 1 datalen number of bytes
child 1 childcount // 4 bytes
sub child 1
sub child 2...
child 2 ID // 4 bytes
child 2 datalen // 4 bytes
child 2 data // child 2 datalen number of bytes
child 2 childcount // 4 bytes
sub child 1...
sub sub child 1 ...
child 3
child 4
sub child 1...

You can then read this just like you wrote it, by reading the first node, getting the number of children in that node, and then calling ReadNode that many times from within itself..

Good luck, its easier than it could be!

This topic is closed to new replies.

Advertisement