title: Saving a Binary tree to a file in C
body: <p>I am currently doing a Customers program when a user can add/edit/search/list customers. I decided to use a binary tree as the backbone of this program. My idea was to then save each item in the tree to "customers.dat" right before the program closes, then on start up load everything from the file to the tree. So far so good, however after finally managing to save the binary search tree into a file, I have one bug. </p>
  
  <p>Lets say I add 3 customers the first time. I then close the program, and when I reopen it I will find the same 3 customers in the tree. however, the next time I open the file, it gives me an error from one of my predefined errors,  which occurs when a node is not able to identity whether to go left or right, maybe because its empty or uncomparable. here are some code snippets. I aslo tried using other fileopen techniques other than a+b, and I had no such error, but by the way I designed my program, I need the append method or else only one record will save.</p>
  
  <p>Customers are stored in Cstmr in the header:</p>
  
  <pre><code>typedef struct customer
  {
      char Name[MAXNAME];
      char Surname[MAXNAME];
      char ID[MAXID];
      char Address[MAXADDRESS];
  
  } Cstmr;
  </code></pre>
  
  <p>else:</p>
  
  <pre><code>void CustomerTreeToFile(Tree*pt)
  {
      if (TreeIsEmpty(pt))
          puts("Nothing to save!");
      else
          Traverse(pt,saveItem); //Traverses each node, and appliess the function 
                                     //saveItem to each node
  }
  
  void saveItem(Cstmr C)
  {
      save = C;
      customers = fopen("customers.dat","ab+");
      fwrite(&amp;C,sizeof (Cstmr), 1, customers);
      fclose(customers);
  }
  </code></pre>