Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Filestream prob...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 7 Topic 9506 of 9775
Post > Topic >>

Filestream problems

by Hani Sharabash <HaniBash@[EMAIL PROTECTED] > Apr 16, 2008 at 11:38 AM

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! ]
 




 7 Posts in Topic:
Filestream problems
Hani Sharabash <HaniBa  2008-04-16 11:38:04 
Re: Filestream problems
Michael.Boehnisch@[EMAIL   2008-04-17 03:49:28 
Re: Filestream problems
Ulrich Eckhardt <eckha  2008-04-17 03:59:44 
Re: Filestream problems
Carl Barron <cbarron41  2008-04-17 03:56:36 
Re: Filestream problems
Hani Sharabash <HaniBa  2008-04-17 16:20:31 
Re: Filestream problems
Carl Barron <cbarron41  2008-04-17 19:35:45 
Re: Filestream problems
"Thomas J. Gritzan&q  2008-04-17 19:34:04 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Jul 8 23:20:59 CDT 2008.