-3

I want to search on a binary file in c++. but this always say: "Not found!".

My codes:

struct {
 char name[20];
 char family[20];
 char address[100];
 char birthday[8];} members[100];

 clrscr();
 pt=fopen("members.dat","r");
 rewind(pt);
 char searchName[20];
 int found=0;
 printf("Please enter search word \n");
 scanf("%s", &searchName);
 int i=0;
 while(!feof(pt)){
 i++;
  fseek (pt,sizeof(members[i]), 0);
  fread(&members[i], sizeof(members[i]), i,pt);
   if(searchName==members[i].name){
     found=1;
     break;}
   }


 clrscr();
 if(found==1){
  printf("Found! \n");
  printf("%s \n", members[1].name);
  printf("%s \n", members[1].family);
  printf("%s \n", members[1].address);
  printf("%s \n", members[1].birthday);
}
 if(found==0){
  printf("Not Found");
 }

what is problem? that's should search name of members struct.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Is this supposed to be C or C++? – Mike Seymour Jan 02 '15 at 13:30
  • you are comparing the pointers to the string, not the strings themselves. – Daniel A. White Jan 02 '15 at 13:30
  • 1
    Also read this http://stackoverflow.com/q/5431941/1870232 – P0W Jan 02 '15 at 13:32
  • this code fails to compile, raising some 40 errors/warnings. Please post code that compiles, how else are we to debug the problem? – user3629249 Jan 02 '15 at 15:01
  • this line: 'if(searchName==members[i].name)' compares addresses. what is really needed is to compare the contents. I.E. use strcmp() or something similar – user3629249 Jan 02 '15 at 15:03
  • the feof() function should not be used as a controling item for a loop, as 1) it is not reliable 2) the initial returned value is not set until after a input I/O operation is performed. – user3629249 Jan 02 '15 at 15:08
  • are you trying to hide the closing braces? '}' for readability (and typical practice) they should be on a separate line, possibly followed by a comment – user3629249 Jan 02 '15 at 15:10
  • the I/O statements, like fopen() and scanf() and fread() and fseek() should have the returned value checked to assure the operation was successful. – user3629249 Jan 02 '15 at 15:13
  • this line: 'fread(&members[i], sizeof(members[i]), i,pt);' reads in the number of member structs as the current value of 'i'. The third parameter should always be 1. This will result in the reading of the later member struct data to overflow the members buffer, resulting in undefined behaviour and possibly a seg fault event. – user3629249 Jan 02 '15 at 15:17
  • this line: 'fseek (pt,sizeof(members[i]), 0);' serves no purpose and should be removed. Also, it always sets the file pointer to the same location (the second member struct data) in the file. And the third parameter should be 'SEEK_SET' not 0 – user3629249 Jan 02 '15 at 15:20
  • all those strings in the member struct will/should have nul byte terminators, within the file, so should be opened as "rb" – user3629249 Jan 02 '15 at 15:25

1 Answers1

1

Use the strcmp function to compare the strings.

 int strcmp(const char *s1, const char *s2);

See the man page for strcmp here.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31