I'm using a bst of strings, the values are added to the bst, then i am trying to print them all to a file. When I do that it prints the first few like 6-12 nodes (the string and a number), however the cout i have right after it prints out everything, while the file stops. Ive debugged and havent found where it could be a logic error.
void saveTreeToFile(node *r, ofstream &file1){
if(r!=NULL){
file1<<r->data<<" "<<r->count<<endl;
cout<<r->data<<" "<<r->count<<endl;
saveTreeToFile(r->left, file1);
saveTreeToFile(r->right, file1);
}
file1.close();
}
thats the code I have which is sending it to the file and printing it out. the function is being passed the root of the bst and the file that it should print too.
I have tried to play around with, when it prints to a file. I have added the cout which works and prints every value out, so that tells me it isn't an error with my recursion. I've tried everything i can think of.
I expect all of the values of the bst to print out to a file, just like it would print out to the console when using the cout.
What I end up getting is like the first 6-12 values and then nothing else, when there should be more like 50 or so on.
I am using large data sets of strings, like the script of Macbeth for example, however in testing i don't use that many words
for reference, here is my main function and the bst code.
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cctype>
using namespace std;
struct node {
string data;
node *left;
node *right;
int count=1;
node(string d){
data=d;
left =NULL;
right=NULL;
};
};
node *root;
int main(){
ifstream file; //tdata -6
file.open("tdata.txt"); //mcb.txt -12
string p;
while(!file.eof()){
file>>p;
p=to_lowercase(p);
p=removePunctuation(p);
if(!findAC(root,p)){
addData(p);
}
}
file.close();
//pInOrder(root);
cout<<endl;
cout<<endl<<getDepth(root)<<endl<<endl;
pPreOrder(root);
//file.close();
ofstream file1;
file1.open("out.txt");
saveTreeToFile(root,file1);
}
Heres the text file i use:
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
With loss of EDEN, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of OREB, or of SINAI, didst inspire
That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
Rose out of CHAOS: Or if SION Hill
Delight thee more, and SILOA'S Brook that flow'd
Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
That with no middle flight intends to soar
Above th' AONIAN Mount, while it pursues
Things unattempted yet in Prose or Rhime.
and the result I get from the file is:
of 7
the 8
tree 1
whose 1
world 1
yet 1
which should be longer, the console print is much longer.
My program does get rid of the punctuation, and makes everything lowercase.
I can include my whole code if needed.