Thursday 16 November 2017

C Program to Update Details of Employee using Files


Code:

#include   stdio.h
#include   stdlib.h
#include   string.h
struct emp
{
    int empid;
    char *name;
};

int count = 0;
void add_rec(char *a);
void display(char *a);
void update_rec(char *a);

void main(int argc, char *argv[])
{
    int choice;
    while (1)
    {
        printf("MENU:\n");
        printf("1.Add a record\n");
        printf("2.Display the file\n");
        printf("3.Update the record\n");
        printf("Enter your choice:");
        scanf("%d", &choice);

        switch(choice)
        {
        case 1:
            add_rec(argv[1]);
            break;
        case 2:
            display(argv[1]);
            break;
        case 3:
            update_rec(argv[1]);
            break;
        case 4:
            exit(0);
        default:
            printf("Wrong choice!!!\nEnter the correct choice\n");
        }
    }
}

void add_rec(char *a)
{
    FILE *fp;
    fp = fopen(a, "a+");
    struct emp *temp = (struct emp *)malloc(sizeof(struct emp));
    temp->name = (char *)malloc(50*sizeof(char));
    if (fp == NULL)
        printf("Error!!!");
    else
    {
        printf("Enter the employee id\n");
        scanf("%d", &temp->empid);
        fwrite(&temp->empid, sizeof(int), 1, fp);
        printf("enter the employee name\n");
        scanf(" %[^\n]s", temp->name);
        fwrite(temp->name, 50, 1, fp);
        count++;
    }
    fclose(fp);
    free(temp);
    free(temp->name);
}

void display(char *a)
{
    FILE *fp;
    char ch;
    int rec = count;
    fp = fopen(a, "r");
    struct emp *temp = (struct emp *)malloc(sizeof(struct emp));
    temp->name = (char *)malloc(50*sizeof(char));
    if (fp == NULL)
        printf("Error!!");
    else
    {
        while (rec)
        {
            fread(&temp->empid, sizeof(int), 1, fp);
            printf("%d", temp->empid);
            fread(temp->name, 50, 1, fp);
            printf(" %s\n", temp->name);
            rec--;
        }
    }
    fclose(fp);
    free(temp);
    free(temp->name);
}

void update_rec(char *a)
{
    FILE *fp;
    char ch, name[5];
    int rec, id, c;
    fp = fopen(a, "r+");
    struct emp *temp = (struct emp *)malloc(sizeof(struct emp));
    temp->name = (char *)malloc(50*sizeof(char));
    printf("Enter the employee id to update:\n");
    scanf("%d", &id);
    fseek(fp, 0, 0);
    rec = count;
    while (rec)
    {
        fread(&temp->empid, sizeof(int), 1, fp);
        printf("%d", temp->empid);
        if (id == temp->empid)
        {
            printf("Enter the employee name to be updated");
            scanf(" %[^\n]s", name);
            c = fwrite(name, 50, 1, fp);
            break;
        }
        fread(temp->name, 50, 1, fp);
        rec--;
    }
    if (c == 1)
        printf("Record updated\n");
    else
        printf("Update not successful\n");
    fclose(fp);
    free(temp);
    free(temp->name);
}


Output:

MENU:
1.Add a record
2.Display the file
3.Update the record
Enter your choice:1
Enter the employee id
1
enter the employee name
aaa
MENU:
1.Add a record
2.Display the file
3.Update the record
Enter your choice:1
Enter the employee id
2
enter the employee name
bbb
MENU:
1.Add a record
2.Display the file
3.Update the record
Enter your choice:3
Enter the employee id to update:
1
1Enter the employee name to be updated1bc
Record updated
MENU:
1.Add a record
2.Display the file
3.Update the record
Enter your choice:2
1 1bc
2 bbb
MENU:
1.Add a record
2.Display the file
3.Update the record
Enter your choice:4



More C programs:
















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...