-1

I have method add, works good, also I have method view and it's work in this method

void search(){


 FILE* fr;
fr=fopen("record.txt","r");
Record r;
char na[10];
 printf(" Enter the name:... ");
    scanf("%s",na);

while(fread(&r,sizeof(r),1,fr)){
if(strcmp(na,r.name))
printf(" A match has been found ... ");
printf("%s %s %s \n",r.name,r.email,r.phoneNum);
}
fclose(fr);}`

I don't know what's the wrong! I have a struct and inside it i have an array of name I ask user to enter names, then write it in a file if I search for specific name the method prints the next name of exact name !

what should I do?

***Record is a struct but I use typedef to rename

1 Answers1

1

You don't have the printf("%s %s %s \n",r.name,r.email,r.phoneNum); within the if brackets so it is going to display each name, not just the matching one.

In addition strcmp will return 0 if it is a match, so currently printf(" A match has been found ... "); is executing when a match is not found.

lostbard
  • 5,065
  • 1
  • 15
  • 17