Program has to read from a file of 10000 names in form of Surname Name(which are seperated by space or tab,fill with them a simple-BST ,print them in order and then search from a file of 1000 names in the BST.
The problem:I should find 900 people after searching.I found 825.
The reason:Some names are saved in BST with some characters that not exist. e.g. We read from file Wu Teresa and this name is saved as Wuax Teresa and Fox Jim as Fox' Jim.
I run at a linux environment.Bellow is my code,sorry for greeklish but i had write in greeklish.Here is the part that i read from stream and save into BST.Reading from search file is done with same way.
Code:
while ( fgets(buf, LINE_LENGTH, fp) != NULL)
{
s=buf;
empty_line=0;
size_for_malloc=0;
until=strchr(s,'\t' ); /* until points to 'tab'*/
if(until == NULL)
{ /* Files from uliko-3hs-askhshs may have an empty line at the end */
until=strchr(s,' '); /* In case names are seperated by whitespace*/
if(until == NULL)
{
empty_line=1;
until=strchr(s,'\n'); /* Case of empty line */
}
if(until == NULL)
{
printf("Sorry,file has to be in form Surname Name\n");
return -1;
}
}
if(empty_line) continue;
while(strcmp(s,until)!=0)/* Go until the end of surname */
{
size_for_malloc++;
s++;
}
counter=0;
while( (*s==' ') || (*s=='\t') )/* Bypass spaces */
{
size_for_malloc++;
s++;
counter++;
}
size_for_malloc-=counter;
counter=1;
line.surname=malloc( (size_for_malloc+1)*sizeof(char) );
strncpy(line.surname,buf,size_for_malloc);
size_for_malloc=0;
until=strchr(s,' ' );
if(until == NULL)
{
until=strchr(s,'\t' );
if(until == NULL)
{
until=strchr(s,'\n' );
}
}
while(strcmp(s,until)!=0)
{
size_for_malloc++;
s++;
}
s-=size_for_malloc;
line.name=malloc( (size_for_malloc+1)*sizeof(char) );
strncpy(line.name,s,size_for_malloc);
count_lines++;
Tree_eisagogi(&BulkTree , line , &error);/* Insert name to BST */
if(error)
{
printf("Sorry,can not allocate memory . Read %u lines\n",count_lines);
return -1;
}
}
Thanks in advance