title: How to search for nodes in BST?
body: <p>I was trying to search for string keys in my BST. </p>
  
  <p>Structure for node and keydata_t as following.</p>
  
  <pre><code>struct node {
  char *key;
  char *details;
  BST* left;
  BST* right;
  };
  
  typedef struct{
   char *name;
   char *data;
  }datatype_t;    
  
  typedef struct{
   char *name;
  }keydata_t;
  </code></pre>
  
  <p>My code for search function: </p>
  
  <pre><code>struct node*search(BST *root, keydata_t *t){
      BST *r = root;  
  
      if (r != NULL){
          printf("here\n");   
      if(strcmp(r-&gt;key, t-&gt;name) == 0){
          printf("found\n");
      }
      else if(strcmp(r-&gt;key,t-&gt;name) &lt; 0){
          printf("left\n");
          return search(r-&gt;left, t);
      }
      else {
          printf("right\n");
          return search(r-&gt;right, t);
  }
  }
  </code></pre>
  
  <p>Insert Function: </p>
  
  <pre><code>struct node*insert(struct node*r, datatype_t *d){
  
  if(r == NULL)
  {
      //printf("empty\n");
      r = (struct node*)malloc(sizeof(struct node));
      r-&gt;key = d-&gt;name;
      r-&gt;details = d-&gt;data; 
      r-&gt;left = r-&gt;right = NULL;
      return r; 
  }
  
  else if(strcmp(r-&gt;key, d-&gt;name ) &lt;= 0){
      r-&gt;left = insert(r-&gt;left ,d);
  }
  else if(strcmp(r-&gt;key, d-&gt;name ) &gt; 0){
      r-&gt;right = insert(r-&gt;right,d);
  }
  return r;
  
  }
  </code></pre>
  
  <p>Read functions:</p>
  
  <pre><code>FILE* safe_open_file(const char* file_name, const char* mode)
  {
  FILE* csv = fopen(file_name,mode);
  if(csv==NULL){
      printf("%s %c ad\n",file_name,*mode);
      exit(EXIT_FAILURE);
      return NULL;
  }
  return csv;
  }
  void printfile(FILE* fp, datatype_t* d)
  {
  char name[64+1];
  char data[1465+1];
  fprintf(fp, "%s--&gt; %s", d-&gt;name, d-&gt;data);
  }
  
  datatype_t*read(FILE* fp)
  {
   char name[66];
   char data[1466];
   if (fscanf(fp, "%[^,] %[^\n]", name, data) == 2) {
      datatype_t *d = (datatype_t*)malloc(sizeof(datatype_t));
      d-&gt;name = strdup(name);
      d-&gt;data = strdup(data);
      return d;
  }
  return NULL;
  }
  keydata_t*read_key(FILE* fp){
  
  char buffer[66];
  if (fgets(buffer,sizeof buffer, fp) != NULL ) {
      keydata_t *k = (keydata_t*)malloc(sizeof(keydata_t));
      size_t len = strlen(buffer);
      if(buffer &gt; 0 &amp;&amp;  buffer[len-1] == '\n'){
          buffer[--len] = '\0';
      }
      k-&gt;name = strdup(buffer);
      return k;
  }
  return NULL;
  }
  </code></pre>
  
  <p>Main function: </p>
  
  <pre><code>int main(int argc, char** argv)
  {
  char* dataset = NULL;
  char* dataset1 = NULL;
  char* key = NULL;
  char* read_mode="r";
  char* write_mode="w+";
  struct node* root = NULL;
  
  if(argv[1]!=NULL &amp;&amp; argv[2] != NULL &amp;&amp; argv[3] != NULL)
  {
      dataset=argv[1];
      dataset1 = argv[2];
      key = argv[3];
  }
  else
  {
      return EXIT_FAILURE;
  }
  FILE* Data_input = safe_open_file(dataset,read_mode);
  FILE* Data_output = safe_open_file(dataset1, write_mode);
  FILE* keyfile = safe_open_file(key, read_mode);
  
  datatype_t **ptr = (datatype_t**)malloc(MAX_NUM_LINE*sizeof(datatype_t *));
  datatype_t *ptr_one;
  keydata_t *keyt;
  
  
  int index = 0;
  while((ptr_one = read(Data_input)) != NULL)
  {
      root = insert(root, ptr_one);
  
  }
  
  while((keyt = read_key(keyfile))!= NULL){
      search(root, keyt);
  }
  
  
  
  
  fclose(Data_input);
  fclose(Data_output);
  fclose(keyfile);
  
  }
  </code></pre>
  
  <p>For some reason it doesn't print found at any point. </p>
  
  <p>These are the keys I am getting from the txt file using fgets.<br><br> 
  Mr neil<br>
  Verizon<br>
  keylor<br></p>
  
  <p>So from my Bst it should return found for the first key but not for the rest. My output wont print anything, it just executes. </p>