I'm writing a program in which I read text from a file, store all of
the words into a binary search tree, and then print the words out to
the screen in order from that tree.
For some reason, opening and using an input filestream is causing
problems for my binary tree functions. When I try to use them
together, my program crashes at runtime. For example:
This works just fine.
#include <iostream>
#include <string>
#include <fstream>
#include "binarytree.h"
#include "treenode.h"
#include "datanode.h"
using namespace std;
int main()
{
string fileName;
string currentWord;
ifstream readIn;
BinaryTree Tree;
currentWord = "abcd";
Tree.insert(currentWord);
Tree.walk();
system("PAUSE");
return 0;
}
However, this does not work.
#include <iostream>
#include <string>
#include <fstream>
#include "binarytree.h"
#include "treenode.h"
#include "datanode.h"
using namespace std;
int main()
{
string fileName;
string currentWord;
ifstream readIn;
BinaryTree Tree;
readIn.open("file.txt");
readIn >> currentWord;
Tree.insert(currentWord);
Tree.walk();
system("PAUSE");
return 0;
}
It seems that the input stream works if there is no tree inserting
going on, and the tree insertion seems to work when there is no input
stream. Now heres the really strange thing. They DO work together in
this case:
int main()
{
string fileName;
string currentWord;
ifstream readIn;
BinaryTree Tree;
string currentWord;
readIn.open("file.txt");
readIn >> currentWord;
string y = "abc";
Tree.insert(y);
Tree.walk();
system("PAUSE");
return 0;
}
but for some reason NOT in this case!
int main()
{
string fileName;
string currentWord;
ifstream readIn;
BinaryTree Tree;
string currentWord;
readIn.open("file.txt");
readIn >> currentWord;
string y = "abcd"; // The only difference is that I'm inserting a
string four letters or more!
Tree.insert(y);
Tree.walk();
system("PAUSE");
return 0;
}
I've tested it thoroughly, for some reason, it only crashes when the
filestream is open and the string I'm inserting is four letters or
more, or is the word from the file (even if its less than four)! What
could possibly be going on?
I can post the code from all the other files if anyone wants to see
it. Also, I apologize in advance if this turns out to be a stupid or
simple question, I'm sort of a novice programmer.
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|