Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

JoshG

Saving Classes to files...

This topic is 6116 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, I am trying to write a program that needs to save information from a Tree. naturally, I''m sure most have come across this before. I have a Binary Tree, with each node being information about a User. so far, in the class I have: ----------------CODE--------- class BTreeNode{ public: BTreeNode *pLow; BTreeNode *pHigh; char *Name; __fastcall BTreeNode(); }; class BTUser: public BTreeNode{ public: char *Password; }; ----------------------------- so I have a Name, a Password, and pointers that are used to navigate the tree. really the pointers have no relevance i don''t think here, as it is the class that I wish to save. So far I am trying to save just the first node, to see if I can get it to work. Basically it isn''t, and it is MOST bizzare. I save it to a file like this... ---------------------CODE--------- FILE *savef = fopen(FileName.c_str(),"w+"); BTUser *save = (BTUser*)dlMain->Users->Head; BTUser temp; temp.pLow = temp.pHigh = NULL; strcpy(temp.Name,save->Name); fwrite(&temp,sizeof(BTUser),1,savef); fclose(savef); --------------------------------- ok... so that seems to work, then I load it, and it mucks up the name and the password!! I load it like this... ------------CODE---------------- -------------------------------- this code works however when I set the values of Password & Name like this... Name = "hello"; Password = "gbye"; Any tips that you have are all welcome!! even different approaches are welcome!! --Josh

Share this post


Link to post
Share on other sites
Advertisement
sorry, the code for loading that I have used is...

FILE *fUsers;
fUsers = fopen(FileName.c_str(),"r");

BTUser *add;
add = new BTUser;

fread(add,sizeof(BTUser),1,fUsers);
ShowMessage(add->Name);

delete add;
fclose(fUsers);

Thanx
--Josh

Share this post


Link to post
Share on other sites
The basic problem you have here is that you are trying to write out a data structure with variable length entries ( eg the strings )

besides that, a char* is just a pointer

also I think your write/read sizes, based on the size of the class, are also likely to cause problems.

To persist a class ( eg write it out ) what you should do is have some sort of methods for writing/reading the class itself.
( there are many different ways of doing this, such as overloading operator<< for streams )

And then, the class should write out the individual data members it needs in order to reconstruct itself correctly.

if a data node''s name and password list is what needs to be written one way to do so ( to a text file ) would be as follows:

1. write out the name
2. write a space
3. write out how many passwords there are
4. write a space
5. write out each password followed by a space

putting the spaces in there will make it easier to read in with iostreams.

Hope this helps

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
if you would just use std::string and std::fstream you''d be ok. Look them up and then you can stop using char* and FILE forever.

Share this post


Link to post
Share on other sites
Another problem is rebuilding the tree when you load it. Assuming you use some sort of recursive save function with your tree, you will have a linear set of data in a file, but due to the nature of the recursive method, you have to write the save function so that it also records the information''s relative position in the tree, or else loading the data into the original tree form is impossible.

Share this post


Link to post
Share on other sites
Yeah, I know that is a problem. however there are recursive functions that will return the nodes in either Pre-Order, in-order, and postorder.
One of those (i'm not sure which) is the order that the nodes were entered into the tree. So i can make the tree in exactly the same tree by adding the nodes in the same order.
The only thing wrong with this is that the tree could be unbalanced, and wouldn't every get balanced. But i'm not particularly worried about that at the moment, I'd really just like to be able to save and load it

I really enjoyed that article on pluggable factories, That has brought new ideas to my mind on ho to fix OTHER problems I have on other projects, but it didn't help so much with this problem.

Edited by - JoshG on December 28, 2001 2:21:45 AM

Share this post


Link to post
Share on other sites
Just use fstream. Once you have a file open on a stream, you can just ''cout << yourObject;'' This will save your object to a file. Then when you ''cin >> yourObject;'', the stream object takes care of everything to make sure it goes in the right spots. Also if you use string instead of char*, you will have an easier time with it.

---
Make it work.
Make it fast.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!