title: Insert function for BST with strings in C
body: <p>This is BST NODE </p>
  
  <pre><code>struct BST_node{
      char *name1;
      char *data1;
      struct BST_node* left;
      struct BST_node* right;
  };   
  
  struct BST_node* Insert(struct BST_node *rootptr, datatype_t *d){
  if(rootptr == NULL){
      char name[66];
      char data[1466];
      rootptr = (struct BST_node*)malloc(sizeof(struct BST_node));
      rootptr-&gt;name1 = d-&gt;name;
      rootptr-&gt;data1 = d-&gt;data;
      printf("%s 1\n", rootptr-&gt;name1);
      rootptr-&gt;left = rootptr-&gt;right = NULL; 
  }
  
  if(strcmp(rootptr-&gt;name1, d-&gt;name) &lt; 0){
      printf("%s left", rootptr-&gt;name1);
      rootptr-&gt;left = Insert(rootptr -&gt; left, d);
  }
  else if(strcmp(rootptr-&gt;name1, d-&gt;name) &gt; 0){
      printf("right\n");
      rootptr-&gt;right = Insert(rootptr -&gt; right, d);
  }
  else if(strcmp(rootptr-&gt;name1, d-&gt;name)==0){
      printf("duplicate\n");
  }
  return rootptr;
  }
  </code></pre>
  
  <p>So I am scanning in a CSV file using file ptrs and reading data in from datatype_d.
  Example this is what the data looks like in the CSV file.
  "Name, Data." </p>
  
  <p>This is how I am reading in the csv file and calling Insert function from my main function.</p>
  
  <pre><code>int main(int argc, char** argv)
  {
  char* eq_csv_file = NULL;
  char* eq_csv_file1 = NULL;
  char* read_mode="r";
  char* write_mode="w+";
  struct BST_node* root = NULL;
  int index = 0;
  
  
  if(argv[1]!=NULL &amp;&amp; argv[2] != NULL )
  {
      eq_csv_file=argv[1];
      eq_csv_file1 = argv[2];
  }
  else
  {
      return EXIT_FAILURE;
  }
  
  FILE* Data_input = safe_open_file(eq_csv_file,read_mode);
  FILE* Data_output = safe_open_file(eq_csv_file1, write_mode);
  
  datatype_t **earth = (datatype_t**)malloc(MAX_NUM_LINE*sizeof(datatype_t *));
  datatype_t *earth_one = read(Data_input);
  
  
  while((earth_one) != NULL)
  {
  
      earth[index] = earth_one;
  
  
      if(root != NULL){
      printf("%s\n", (root-&gt;name1));
  
      }
      root = Insert(root, earth[index]);
  
      index++;
      earth_one= read(Data_input);
  }
  </code></pre>
  
  <p>Now when I am running through this code to check if its working, its printing out Duplicate for all the data from the csv file except for the first data. I simply dont know where exactly is that I am making a mistake and changing the rootptr and making it equal to datatype_d that its printing duplicate when it checks the strcmp.</p>
  
  <p>So an example would be that I read this from a CSV file. </p>
  
  <p>"Dave", "Studying at UCLA"<br>
  "John", "Works at Google"<br>
  "Mike", "School teacher" <br></p>
  
  <p>So I am suppose to insert these into the BST but for some reason, its not keeping track of the head node "Dave". It never goes to the left or right subtree but rather it says that they are duplicates when I use strcmp. </p>
  
  <p>Here is the output:<br>
  Dave 1 <br>
  John <br>
  duplicate<br>
  Segmentation fault: 11<br></p>
  
  <p>This is my read code which I call from the main function to read the csv file.</p>
  
  <pre><code>read(FILE* fp)
  {
  char name[65];
  char data[1465];
  if (fscanf(fp, "%[^,] %[^\n]", name, data) == 2) {
      datatype_t *d = (datatype_t*)malloc(sizeof(datatype_t));
      d-&gt;name = name;
      d-&gt;data = data;
      return d;
  }
  return NULL;
  }
  </code></pre>