title: how to write a correct search function in C?
body: <p>i have a problem in 'search' function, keep showing 'record not found' i have successfully save it into file and i can display it. can someone tell me in which part i wrote it wrong?</p>
  <p><a href="https://i.stack.imgur.com/WMYQX.png" rel="nofollow noreferrer">enter image description here</a></p>
  <pre><code>typedef struct vaccine{
      char vacname[20]; 
      char code[5];
      char country[20];
      int dosage, quantity;   
      float population;
  }vaccine;
  
  void addVaccine(){
      vaccine *v;
      FILE *fp;
      int n, i;
      printf(&quot;Enter how many vaccine you want: &quot;);
      scanf(&quot;%d&quot;,&amp;n);
  
      v= (vaccine*)calloc(n, sizeof(vaccine));
      fp = fopen(&quot;vaccine.txt&quot;,&quot;w&quot;);
  
      for(i=0;i&lt;n;i++){
          printf(&quot;Enter Vaccine Name: &quot;);
          scanf(&quot;%s&quot;, v[i].vacname);
          printf(&quot;Enter Code: &quot;);
          scanf(&quot;%s&quot;, v[i].code);
          printf(&quot;Enter Country: &quot;);
          scanf(&quot;%s&quot;, v[i].country);
          printf(&quot;Enter Dosage: &quot;);
          scanf(&quot;%d&quot;, &amp;v[i].dosage);
          printf(&quot;Enter Quantity: &quot;);
          scanf(&quot;%d&quot;, &amp;v[i].quantity);
          printf(&quot;Enter Population: &quot;);
          scanf(&quot;%.2f&quot;, &amp;v[i].population);    
  
          fwrite(&amp;v[i],sizeof(vaccine),1,fp);
      }
      fclose(fp);
  }
  </code></pre>
  <p>here is my search function which i have problem with;</p>
  <pre><code>void search(){
      vaccine v;
      FILE *fp;
      int i, code, found=0;
      fp = fopen(&quot;vaccine.txt&quot;,&quot;r&quot;);
  
      printf(&quot;Enter code to search: &quot;);
      scanf(&quot;%s&quot;, &amp;code);
  
      while(fread(&amp;v,sizeof(vaccine),1,fp))
      { 
          if(v.code == code){
          found=1;
          printf(&quot;\n%s\t%s\t%d&quot;,
          v.vacname,v.code,v.quantity);
          }
      }
      if(!found)
      printf(&quot;\nRecord Not Found&quot;);
      fclose(fp);
  }
  </code></pre>