title: Edit/Modify & Delete of record from a Random Access File in C
body: <p>The program should modify or delete a specific record according to employee id, in modifying part it writes the modified record as a new one at the end of the file, the deleting part works only once and then give me a segmentation fault.</p>
  
  <p>Modifying:</p>
  
  <p>How can I modify the code to rewrite the edited record in the same position?</p>
  
  <pre><code> #include &lt;stdio.h&gt;
   #include &lt;string.h&gt;
   #include &lt;stdlib.h&gt;
   #include &lt;stdlib.h&gt;
  
  struct record_em{
      int id;
      char name[20];
      int salary;
      int age;
  };
  
  int main( void )
  {
      struct record_em employee;
      FILE *fp;
      int n;
      int ch;
      fp = fopen("empRecord.dat","rb+");
  
      printf("Enter Id Number:\n");
      scanf("%d",&amp;n);
      rewind(fp);
      while (!feof(fp)){
  
          fscanf(fp,"%d %s %d %d", &amp;employee.id, employee.name, &amp;employee.salary, &amp;employee.age);
  
          if (employee.id==n){
              printf("%d %s %d %d \n",employee.id, employee.name, employee.salary,employee.age);
              printf("\n Do you want to change the name ?\n");
              scanf("%d",&amp;ch);
  
              if (ch==1){
                  printf("Enter new name:\n");
                  scanf("%s",employee.name);
              }
              printf("\n Do you want to change the salary ?(y/n)\n");
              scanf("%d",&amp;ch);
  
              if ( ch==2 ){
                  printf("Enter new salary:\n");
                  scanf("%d",&amp;employee.salary);
              }
              printf("\n Do you want to change the age ?(y/n)\n");
              scanf("%d",&amp;ch);
  
              if ( ch==3 ){
                  printf("Enter new age:\n");
                  scanf("%d",&amp;employee.age);
              }
              fseek(fp,-sizeof(employee),SEEK_CUR);
              fprintf(fp, "%d %s %d %d\n", employee.id, employee.name, employee.salary, employee.age);
              exit(0);
          }
      }
      printf("Record Not Found \n");
      return 0;
  }
  </code></pre>
  
  <p>Deleting:</p>
  
  <p>How can I modify the code to make it delete records as many times as I want?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct record_em{
      int id;
      char name[20];
      int salary;
      int age;
  };
  
  int main()
  {
      struct record_em employee;
      FILE *fp, *ft;
      int n;
      fp = fopen("empRecord.dat","r");
      ft = fopen("Temp.dat","wb+");
  
      printf("\nEnter ID of employee to delete ");
  
      scanf("%d",&amp;n);
      rewind(fp);
      while (!feof(fp)){
          fscanf(fp,"%d %s %d %d", &amp;employee.id, employee.name, &amp;employee.salary,  &amp;employee.age);
  
          if(employee.id!=n){
              fprintf(ft, "%d %s %d %d\n", employee.id, employee.name, employee.salary, employee.age);
          }
      }
  
      fclose(fp);
      fclose(ft);
      remove("empRecord.dat");
      rename("Temp.dat","EempRecord.dat");
  
      return 0;
  }
  </code></pre>