title: Modify an existing line in file and trim the rest of the line
body: <p>Please help me, I made a function to modify a line in file using C language. Now my problem is that although the line is modified, the rest of the line I modified is still as is; For example if the new data I enter is shorter than the original line, my new data will be written, but the rest of the line will still be filled with old data. Please check the first line in the outputs below. Please advise me, thank you very much.</p>
  <p>Original file:</p>
  <pre><code>Sam,Thomas,10-06-1995,26 Elhoreya Street,01234567899,sthomas@gmail.com
  Steven,Thomas,10-06-1995,26 Elhoreya Street,01234567899,sthomas@gmail.com
  </code></pre>
  <p>Expected output file:</p>
  <pre><code>John,George,12-1-2000,23 MN Street,1234567899,gJoh@gmail.com
  Steven,Thomas,10-06-1995,26 Elhoreya Street,01234567899,sthomas@gmail.com
  </code></pre>
  <p>Real output file:</p>
  <pre><code>John,George,12-1-2000,23 MN Street,1234567899,gJoh@gmail.com@gmail.com
  Steven,Thomas,10-06-1995,26 Elhoreya Street,01234567899,sthomas@gmail.com
  </code></pre>
  <p>This is my function:</p>
  <pre><code>void modify(contact c[])
  {
      int flag=0, k=0, count_matches=0, match_cont_pos[100], choose;
      char name[50], temp;
  
      FILE * file;
      file = fopen(&quot;info.txt&quot;, &quot;rb+&quot;);
  
      if(file == NULL)
      {
          printf(&quot;Error opening file\n&quot;);
          exit(1);
      }
  
      else
      {
          printf(&quot;\nEnter the name you want to search: &quot;);
          scanf(&quot;%s&quot;,&amp;name);
  
          for(int j = 0; name[j] != '\0'; j++)
              name[j] = tolower(name[j]);
  
          for(int i = 0; i &lt; count ; i++) {
  
              for(int j = 0; c[i].last_name[j] != '\0'; j++)
                  c[i].last_name[j] = tolower(c[i].last_name[j]);
  
              if(strcmp(c[i].last_name,name) == 0)
              {
                  match_cont_pos[k]=i;
                  k++;
              }
  
          }
          for(int d=0;d&lt;k;d++)
              printf(&quot;match pos %d\n&quot;,match_cont_pos[d]);
  
          for(int d=0;d&lt;k;d++){
              printf(&quot;\n%s\n%s\n%d %d %d\n%s\n%d\n%s\n\n&quot;, c[match_cont_pos[d]].last_name,c[match_cont_pos[d]].first_name,c[match_cont_pos[d]].DoB.day,c[match_cont_pos[d]].DoB.month,c[match_cont_pos[d]].DoB.year,c[match_cont_pos[d]].street_address,c[match_cont_pos[d]].phone_number,c[match_cont_pos[d]].email);
          }
  
          if(k&gt;1){
  
              printf(&quot;Enter the number of the record you want to modify: &quot;);
              scanf(&quot;%d&quot;,choose);
  
              printf(&quot;Enter last name: &quot;);
              scanf(&quot;%s&quot;,&amp;c[match_cont_pos[choose-1]].last_name);
              printf(&quot;Enter first name: &quot;);
              scanf(&quot;%s&quot;,&amp;c[match_cont_pos[choose-1]].first_name);
              printf(&quot;Enter phone number: &quot;);
              scanf(&quot;%d&quot;,&amp;c[match_cont_pos[choose-1]].phone_number);
              printf(&quot;Enter email: &quot;);
              scanf(&quot;%s&quot;,&amp;c[match_cont_pos[choose-1]].email);
              printf(&quot;Enter birth date: &quot;);
              scanf(&quot;%d %d %d&quot;,&amp;c[match_cont_pos[choose-1]].DoB.day,&amp;c[match_cont_pos[choose-1]].DoB.month,&amp;c[match_cont_pos[choose-1]].DoB.year);
              printf(&quot;Enter the address: &quot;);
              scanf(&quot;%c&quot;,&amp;temp); // temp statement to clear buffer
              scanf(&quot;%[^\n]&quot;,&amp;c[match_cont_pos[choose-1]].street_address);
  
              fseek(file,-sizeof(c),SEEK_SET);
  
              fprintf(file,&quot;%s,%s,%d-%d-%d,%s,%d,%s&quot;,c[match_cont_pos[choose-1]].last_name,c[match_cont_pos[choose-1]].first_name,c[match_cont_pos[choose-1]].DoB.day,c[match_cont_pos[choose-1]].DoB.month,c[match_cont_pos[choose-1]].DoB.year,c[match_cont_pos[choose-1]].street_address,c[match_cont_pos[choose-1]].phone_number,c[match_cont_pos[choose-1]].email);
          }
  
      }
      fclose(file);
  }
  
  </code></pre>