title: file pointer and instance of struct in a function
body: <p>I'm trying to study on Textfile topic and I find myself not fully understand how does the declaration of two pointer point to file in void function name modify() work. what I understand is first I need to declare pointer for each file (t1 for item.dat) and (t for tBat.dat). t1 is previously assigned the value by the previous void function name append. then I need to declare an instance of struct for each file. how the modify function work is to read the data from item.dat ( &quot;rb&quot; mode ) and write it on the tBat.dat ( &quot;wb&quot; mode ) then do the modifying on tBat.dat when finished assigned the data back to item.dat by swap the file mode item.dat ( &quot;wb&quot; mode ) tBat.dat ( &quot;rb&quot; mode ). Do I understand the code correctly? If I'm not please correct me. <a href="https://i.stack.imgur.com/MgXqN.jpg" rel="nofollow noreferrer">enter image description here</a></p>
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  
  struct Bat
  {
      int id;
      char name[40];
      int price;
      int stock;
      int buy;
      int left;
  };
  
  void append();
  void modify();
  
  char fname[]= {&quot;item.dat&quot;};
  
  int main()
  {
      int ch;
  
      while(1)
      {
  
          printf(&quot;==================Batloyee Management System=============\n\n&quot;);
  
          printf(&quot;1. Append\n\n&quot;);
          printf(&quot;2. Modify\n\n&quot;);
          printf(&quot;any other to Exit\n\n&quot;);
  
          printf(&quot;========================================================\n\n&quot;);
  
          printf(&quot;\nPlease enter your Choice:&quot;);
          scanf(&quot;%d&quot;,&amp;ch);
  
          switch(ch)
          {
          case 1:
              append();
              break;
  
          case 2:
              modify();
              break;
  
          default:
              printf(&quot;exist&quot;);
              return 0;
          }
      }
  
      return 0;
  }
  
  void append()
  {
      FILE *fp;
      struct Bat t, t1;
  
      fp=fopen(fname,&quot;ab&quot;);
  
      printf(&quot;\nEnter ID:&quot;);
      scanf(&quot;%d&quot;,&amp;t1.id);
      printf(&quot;\nEnter name:&quot;);
      scanf(&quot; %[^\n]&quot;,t1.name);
      printf(&quot;\nEnter price:&quot;);
      scanf(&quot;%d&quot;,&amp;t1.price);
      printf(&quot;\nEnter amount:&quot;);
      scanf(&quot;%d&quot;,&amp;t1.stock);
      fwrite(&amp;t1,sizeof(t1),1,fp);
      fclose(fp);
  }
  
  
  
  void modify()
  {
      FILE *fp,*fp1;
      struct Bat t,t1;
      int id,found=0,count=0;
  
      fp=fopen(fname,&quot;rb&quot;);
      fp1=fopen(&quot;tBat.dat&quot;,&quot;wb&quot;);
  
      printf(&quot;\nEnter the Bat ID you want to Modify:&quot;);
      scanf(&quot;%d&quot;,&amp;id);
  
      while(1)
      {
          fread(&amp;t,sizeof(t),1,fp);
  
          if(feof(fp))
          {
              break;
          }
          if(t.id==id)
          {
              found=1;
              printf(&quot;\nEnter Batloyee ID:&quot;);
              scanf(&quot;%d&quot;,&amp;t.id);
  
              //fflush(stdin);
              printf(&quot;\nEnter Batloyee Name:&quot;);
              scanf(&quot; %[^\n]&quot;,t.name);
              printf(&quot;\nEnter Batloyee Price:&quot;);
              scanf(&quot;%d&quot;,&amp;t.price);
              printf(&quot;\nEnter amount:&quot;);
              scanf(&quot;%d&quot;,&amp;t.stock);
              fwrite(&amp;t,sizeof(t),1,fp1);
          }
          else
          {
              fwrite(&amp;t,sizeof(t),1,fp1);
          }
      }
      fclose(fp);
      fclose(fp1);
  
      if(found==0)
      {
          printf(&quot;Sorry No Record Found\n\n&quot;);
      }
      else
      {
          fp=fopen(fname,&quot;wb&quot;);
          fp1=fopen(&quot;tBat.dat&quot;,&quot;rb&quot;);
  
          while(1)
          {
              fread(&amp;t,sizeof(t),1,fp1);
  
              if(feof(fp1))
              {
                  break;
              }
              fwrite(&amp;t,sizeof(t),1,fp);
          }
  
      }
      fclose(fp);
      fclose(fp1);
  }  
  </code></pre>