title: Reading from file into binary tree and writing to a file
body: <p>I'm reading into a binary tree a file named "Lincoln.txt" and putting them into my tree in alphabetic order and I'm supposed to write the word and the count of the word to a file named "index.txt". Now, I can read them into my binary tree no problem, but I'm not quite sure how to write them into the "index.txt". I check my file after it's finished and all I get are addresses. Could you help me out?</p>
  
  <p>Here is my coding:</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "NodeClass.h"
  #include &lt;fstream&gt;
  
  using namespace std;
  
  int main()
  {
      string myString;
      BinaryTree myTree("");
      ifstream infile;
  
      infile.open("Lincoln.txt");
  
      while(infile)
      {
          infile &gt;&gt; myString;
          myTree.insert(myString, myTree.root);
      }
  
      ofstream outFile;
  
      outFile.open("index.txt");
  
      myTree.print(myTree.root, outFile);
  
      outFile.close();
  }
  </code></pre>
  
  <p>NodeClass.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "NodeClass.h"
  
  using namespace std;
  
  void BinaryTree::insert(ElementType data, TreeNode *&amp;tree)
  {
      if (tree == NULL)
      {
          tree = new TreeNode(data);
      }
      else if(data &lt; tree-&gt;data)
          insert(data, tree-&gt;left);
      else if(data &gt; tree-&gt;data)
          insert(data, tree-&gt;right);
      else if(data == tree-&gt;data)
          tree-&gt;count++;
  
  }
  
  void BinaryTree::display(TreeNode *tree, ostream&amp; out)
  {
      if (tree != NULL)
      {
          display(tree-&gt;left, out);
          out &lt;&lt; tree-&gt;data&lt;&lt;" ";
          display(tree-&gt;right, out);
      }
  }
  
  void BinaryTree::print(TreeNode *tree, ofstream&amp; outFile)
  {
  
      if (tree != NULL)
      {
          outFile &lt;&lt; tree-&gt;left;
          outFile &lt;&lt; tree-&gt;data &lt;&lt; " " &lt;&lt; tree-&gt;count;
          outFile &lt;&lt; tree-&gt;right;
      }
  
  }
  </code></pre>
  
  <p>NodeClass.h</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;fstream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  #ifndef TREENODE_H
  #define TREENODE_H
  
  typedef std::string ElementType;
  
  class BinaryTree
  {
  public:
      class TreeNode
      {
      public:
          ElementType data;
          int count;
          TreeNode *left;
          TreeNode *right;
          TreeNode(ElementType new_data)
              { data = new_data; left = NULL; right = NULL;};     
      };
  TreeNode *root;
  BinaryTree(ElementType root_data)
      { root = new TreeNode(root_data); };
  ~BinaryTree() { delete root;};
  void insert(ElementType data, TreeNode *&amp;tree);
  void display(TreeNode *tree, ostream&amp; out);
  void print(TreeNode *tree, ofstream&amp; outFile);
  };
  
  #endif TREENODE_H
  </code></pre>
  
  <p>Lincoln.txt</p>
  
  <pre><code> The Gettysburg Address
  
  
  
                  Gettysburg, Pennsylvania
  
                 November 19, 1863 
  
  
  
  
  
  Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in
  
  Liberty, and dedicated to the proposition that all men are created equal. 
  
  
  
  Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and 
  
  so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate 
  
  a portion of that field, as a final resting place for those who here gave their lives that that nation 
  
  might live. It is altogether fitting and proper that we should do this. 
  
  
  
  But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. 
  
  The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add 
  
  or detract. The world will little note, nor long remember what we say here, but it can never forget what 
  
  they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they 
  
  who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great 
  
  task remaining before us -- that from these honored dead we take increased devotion to that cause for 
  
  which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not 
  
  have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government 
  
  of the people, by the people, for the people, shall not perish from the earth. 
  </code></pre>
  
  <p>After writing to index.txt, this is what I get:</p>
  
  <pre><code>00000000 -8421504510035D230
  </code></pre>
  
  <p>EDIT: My professor says my print function should be a recursive function, but I don't know how to do that.</p>