-
saving a binary tree??
Hiya!
I've been making a binary tree over the last couple of days, and i've got stuck on saving it! I need to save it and then read it back into memory when i load the program up.
Obviously doing a pre-order traversal, but it wont work, when using recursion and I/O do you have to change things?
I've got a feeling i'm missing something really simple! I had a search and coulddnt find anyone with a similar propblem.
Cheers for any advice!
snipet of my code:
void saveIndexINT()
{
saveIndex(root);
}
void saveIndex(node *currentNode)
{
ofstream indexOut;
indexOut.open("indexData.txt", ios:: out); <-----emoticons!!! ignore the space
if(currentNode != NULL)
{
indexOut << currentNode->name << "*" << currentNode->recNo << endl;
saveIndex(currentNode->left);
saveIndex(currentNode->right);
}
indexOut.close();
}
-
You are missing something simple. Code tags.
Quzah.
-
ahh, square brackets! thanks for the tip!
Code:
void saveIndexINT()
{
saveIndex(root);
}
void saveIndex(node *currentNode)
{
ofstream indexOut;
indexOut.open("indexData.txt", ios:: out); <-----emoticons!!! ignore the space
if(currentNode != NULL)
{
indexOut << currentNode->name << "*" << currentNode->recNo << endl;
saveIndex(currentNode->left);
saveIndex(currentNode->right);
}
indexOut.close();
}
-
indexOut.open("indexData.txt", ios:: out); <-----emoticons!!! ignore the space
The above line opens indexData.txt each time you call saveIndex(). The problem is each time it opens the file it overwrites the contents. Therefore, I'd open indexData just once back in main() or somewhere before saveIndex() and pass the stream associated with the file to saveIndex() with the node you want saved. That way you keep adding to the file with each call to saveIndex instead of overwriting it each time you call saveIndex.
-
Like this
http://cboard.cprogramming.com/showt...888#post433888
Only following left and right pointers at each node.
-
cheers! I understand what you mean, thats what i was thinking myself
so.........
Code:
void saveIndexINT()
{
ofstream indexOut;
indexOut.open("indexData.txt", ios::out);
saveIndex(root, "indexData.txt");
indexOut.close();
}
void saveIndex(node *currentNode, char *filename)
{
//indexOut << numberOfRecords << endl;
if(currentNode != NULL)
{
indexOut << currentNode->name << "*" << currentNode->recNo;
saveIndex(currentNode->left, "indexData.txt");
saveIndex(currentNode->right, "indexData.txt");
}
}
I create the "indexData.txt" in the procedure before hand, I then pass it to my current procedure.
I found the code you showed me abit confusing, fwrite etc, not sure how to use those, can it be done using the read and write functions?
-
It works!
Got it working! cheers.
As a global i declared:
ofstream indexOut("indexData.txt", ios::out);
then just used it in the procedures. So simple.
fwrite and co, thats from C isnt it?
Only just really started with C++ (as you can see) and programing in general. Its fun when you get something working! Just as frustrating when it dosent work tho!
Cheers again for your help, much appreciated!
-
> can it be done using the read and write functions?
Yes, C++ fstreams have read() and write() methods
-
Note: the stream doesn't have to be global.
Code:
void saveIndex(ofstream & fout, node * currentNode);
int main()
{
ofstream fout("indexData.txt");
//create tree including a root node
//send root node of tree to saveIndex() to start saving to file
saveIndex(fout, root);
//etc.
}
void saveIndex(ofstream & fout, node * currentNode)
{
if(currentNode != NULL)
{
fout << currentNode->name << "*" << currentNode->recNo;
saveIndex(fout, currentNode->left);
saveIndex(fout, currentNode->right);
}
}
Alternatively, if you wanted to open and close the stream with each call to saveIndex() you could open the stream in ios::app mode instead of the default ios::trunc mode. To me opening and closing stream with each call to saveIndex() seemed overkill is all. I prefer to send the stream to the function by reference as opposed to using a global variable to hold the stream.
-
ahh!
ah!! Thats what i actually wanted to do, we've been told: avoid global variables, encapsulation etc etc. I just didnt know how to do it!
Cheers for that!