Edit/Modify & Delete of record from a Random Access File in C

fatimah picture fatimah · Nov 24, 2012 · Viewed 28.2k times · Source

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.

Modifying:

How can I modify the code to rewrite the edited record in the same position?

 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdlib.h>

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",&n);
    rewind(fp);
    while (!feof(fp)){

        fscanf(fp,"%d %s %d %d", &employee.id, employee.name, &employee.salary, &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",&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",&ch);

            if ( ch==2 ){
                printf("Enter new salary:\n");
                scanf("%d",&employee.salary);
            }
            printf("\n Do you want to change the age ?(y/n)\n");
            scanf("%d",&ch);

            if ( ch==3 ){
                printf("Enter new age:\n");
                scanf("%d",&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;
}

Deleting:

How can I modify the code to make it delete records as many times as I want?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdlib.h>

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",&n);
    rewind(fp);
    while (!feof(fp)){
        fscanf(fp,"%d %s %d %d", &employee.id, employee.name, &employee.salary,  &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;
}

Answer

Sachin Kumar picture Sachin Kumar · Nov 30, 2013

Here is the Key point of code:

void update(char filename[],char name[])
{
    int records=0;
    FILE *fp = fopen(filename,"rb+");
    while(fread(&st,sizeof(st),1,fp)==1)
    {
         if(strcmp(name,st.name)==0)
         {
              printf("\nEnter new name: ");
              scanf("%s",st.name);
              printf("\nEnter new roll no.: ");
              scanf("%d",&st.roll);
              fseek(fp,sizeof(struct student)*records,SEEK_SET);//This is key line..
              fwrite(&st,sizeof(st),1,fp);
         }
         records++; // in the while loop...
    }
    fclose(fp);
}

Below is the structure of student:

struct student{
     int roll;
     char name[20];
}st;

This is the general method to modify/update the record. You can use same syntax for your employee structure.