Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

save structure to file

Status
Not open for further replies.

Leibnitz

Programmer
Apr 6, 2001
393
CA
******************************************************************************
I have used the following code sample to save a tree structure to a file:
Code:
fp = fopen( "tree.dat", "wb" );
if( !fp ) {
     cerr << &quot;Error while saving tree&quot; << endl;
     break;
}
fwrite( &tr, sizeof(tr), 1, fp );
but when i load back the structure from the file,i dont get any data.
To load the structure,i use:
Code:
fp = fopen( &quot;tree.dat&quot;, &quot;rb&quot; );
if( !fp ) {
    cerr << &quot;Error while loading tree&quot; << endl;
    break;
}
fread( &tr, sizeof(tr), 1, fp )
And finaly,the code i've used to create the tree is:
Code:
#include <algorithm>
#include <string>
#include <iostream>
#include &quot;tree.hh&quot;
using namespace std;

....
.....
tree<string> tr;
tree<string>::iterator top, one, two, loc, banana;

top=tr.begin();
one=tr.insert(top, &quot;one&quot;);
two=tr.append_child(one, &quot;two&quot;);
tr.append_child(two, &quot;apple&quot;);
banana=tr.append_child(two, &quot;banana&quot;);
tr.append_child(banana,&quot;cherry&quot;);
tr.append_child(two, &quot;peach&quot;);
tr.append_child(one,&quot;three&quot;);
...

the file header &quot;tree.hh&quot; is an STL-like container class for n-ary trees,it can be found in the following address:
 
Read my post about structures in this thread: thread205-622736

C++ classes are even more complex in terms of memory addresses than the C style
Code:
structure
being discussed in that thread. In your code you are only writing some compiler generated values like &quot;this&quot; pointers and pointers to virtual tables etc. Not at all what you desire.

-pete
 
Thanks for replying! I think that i might have to write some specific functions to write and read the tree structure to the file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top