Member Avatar for Lioshenka

Hi,

I already asked but haven't had an answer.

What I am trying to do is to write a program which will read the data from the file into BST with the ability to search by surname (surname is the key). After sleepless nights I have managed to do something, but I don't think it does what I want. Can someone please guide me in the right direction?

Does that code even read the data to a structure with surname as the key, because to me it seems it only stored first letters of the surname in BST.

Thanks a lot!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct tree {
  char info;
  struct tree *left;
  struct tree *right;
};

struct tree *root; /* first node in tree */
struct tree *stree(struct tree *root,
                   struct tree *r, char info);
void print_tree(struct tree *root, int l);


int main()
{
     typedef struct{
            char dummy[256];
            char surname[225];
            char name[256];
            int ID;
            char house[256];
            char street[256];
            char town[256];
            char county[256];
            char area[256];
            char country[256];
            char postcode[256];
            int phone;
            char email[256];
            char boattype[256];
            char boatname[256];
            }user;
root = NULL;  /* initialize the root */
FILE *file;
int i=0;
char number_of_people[256];
int nop;
char yetdummy[256]; 
char yetdummy2[256]; 
char s[80];

  file = fopen("members.data","r");
 if(file==NULL) {
    printf("Error: can't open file.\n");
    return 1;
  }
  else {
    printf("File opened successfully.\n");
         }
 fscanf(file,"%s \n", number_of_people);
 //printf("%s \n", number_of_people);

 nop = atoi(number_of_people);
 //printf("%d\n", nop);
 user usera[nop]; 
 
  for (i=0; i<nop; i++){
  printf("Reading a structure...\n");
      fgets(usera[i].dummy, 256, file);        
      fgets(usera[i].surname, 256, file);  
      fgets(usera[i].name, 256, file);  
      fgets(yetdummy, 256, file);  
      usera[i].ID = atoi(yetdummy);      
      fgets(usera[i].house, 256, file);  
      fgets(usera[i].street, 256, file);  
      fgets(usera[i].town, 256, file); 
      fgets(usera[i].county, 256, file);  
      fgets(usera[i].area, 256, file);  
      fgets(usera[i].country, 256, file);  
      fgets(usera[i].postcode, 256, file);  
      fgets(yetdummy2, 256, file); 
      usera[i].phone = atoi(yetdummy2);
        
      fgets(usera[i].email, 256, file);  
      fgets(usera[i].boattype, 256, file);  
      fgets(usera[i].boatname, 256, file);        
     
     printf("Name is %s\n", usera[i].name);      
      printf("ID is %d\n", usera[i].ID); 
       root = stree(root, root, *usera[i].surname); 
    }
    fclose(file);

    
  //print_tree(root, 0);

  return 0;
}

struct tree *stree(
  struct tree *root,
  struct tree *r,
  char info)
{

  if(!r) {
    r = (struct tree *) malloc(sizeof(struct tree));
    if(!r) {
      printf("Out of Memory\n");
      exit(0);
    }
    r->left = NULL;
    r->right = NULL;
    r->info = info;
    if(!root) return r; /* first entry */
    if(info < root->info) root->left = r;
    else root->right = r;
    return r;
  }

  if(info < r->info)
    stree(r, r->left, info);
  else
    stree(r, r->right, info);
 
return root;
}
char info;

info is not a string, it is a single char. That explains why you only get the first character of the surname. Change info to an array of 255 like the surname array in the user structure, or change the type to an object of the user structure:

typedef struct{
    char dummy[256];
    char surname[225];
    char name[256];
    int ID;
    char house[256];
    char street[256];
    char town[256];
    char county[256];
    char area[256];
    char country[256];
    char postcode[256];
    int phone;
    char email[256];
    char boattype[256];
    char boatname[256];
}user;

struct tree {
  user info;
  struct tree *left;
  struct tree *right;
};

This should give you a good start for making the code do what you want. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.