line crashes

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);
                }
           }
The code as it stands look ok, if inFile is of type std::istream/std::ifstream/...

And lineFile and word are of type std::string

But I think the code which is causing you grief is elsewhere!
Here is the rest of it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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.

Thanks
Got it! I just needed to go back a few loops :)
Line 22, you read in a line from the file.

You never use it, so if you're looking for a word that occurs in the first line of the file, you're not going to find it.

You're attempting to close the same file twice. (line 40/42)

I don't see anything that looks like it would cause a crash outside of, possibly, the binSearchTree insert method about which we know nothing.






Topic archived. No new replies allowed.