I am trying read a text file and get each word into a node in a binary search tree, however, it crashes halfway through the file and I can't figure out why. Is there something I'm need to add?
1 2 3 4 5 6 7 8 9 10 11 12
while(getline(inFile,lineFile))//inFile: file //lineFile: line in file
{
//convert text in file to lower case
toLowerCase(lineFile);
//get each word into a node in a bst
while (inFile >> word)
{
cout << word << endl;
tree.insert(word);
}
}
int main()
{
binSearchTree tree;
ifstream inListFile, inFile;
string filename, lineFile;
string word;
inListFile.open("listfilename.txt"); //Open a file from your directory
if (inListFile.is_open()) //Check if the file was open correctly else 'Unable to open file'.
{
while (getline(inListFile, filename)) //Loop iterates through the 'listfilename.txt using getline()'
{
//cout << "'" << filename << "'" << endl; //Print a line from 'listfilename.txt'
inFile.open(filename.c_str());
if(inFile.is_open() )
{
//cout << "File Name: "<< filename << endl;
while(getline(inFile,lineFile))//inFile: file //lineFile: line in file
{
//convert text in file to lower case
toLowerCase(lineFile);
//get each word into a node in a bst
while (inFile >> word)
{
cout << word << endl;
tree.insert(word);
}
}
}
else cout << "Unable to open file";
inFile.close();
}
inFile.close();
}
else cout << "Unable to open list file\n";
return 0;
}
It is structured the way it is because I needed to open a bunch of files, one after another, so that I could search for a word in them, so in listfilename.txt, it lists the name of the files to iterate through. I am trying to read a file, put it's words , one per node in a binary tree so that I can search for a word and report back which file it was found in but I cant get past the crashing part.