Tuesday, December 9, 2014

Update Details of Employ ee using Files in c



Update Details of Employee using Files

#include 
#include 
#include 
#include 
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

Create Employee File Name Record that is taken from the Command Line Argument

#include 
#include 
#include 
#include 
#include 
 
struct emprec
{
    int empid;
    char *name;
};
typedef struct emprec emp;
 
void insert(char *a);
void display(char *a);
void update(char *a);
int count;
void main(int argc, char *argv[])
{
    int choice;    
    while (1)
    {
        printf("Enter the choice\n");
        printf("1-Insert a new record into file\n2-Display the records\n");
        printf("3-Update the record\n4-Exit\n");
        scanf("%d",  &choice);
        switch (choice)
        {
        case 1:
            insert(argv[1]);
            break;
        case 2:
            display(argv[1]);
            break;
        case 3:
            update(argv[1]);
            break;
        case 4:
            exit(0);
        default:
            printf("Enter the correct choice\n");
        }
    } 
}
 
void insert(char *a)
{
    FILE *fp1;
    emp *temp1 = (emp *)malloc(sizeof(emp));
    temp1->name = (char *)malloc(200 * sizeof(char)); 
 
    fp1 = fopen(a, "a+");
    if (fp1 == NULL)
        perror("");
    else
    {
        printf("Enter the employee id\n");
        scanf("%d", &temp1->empid);
        fwrite(&temp1->empid, sizeof(int), 1, fp1);
        printf("Enter the employee name\n");
        scanf(" %[^\n]s", temp1->name);
        fwrite(temp1->name, 200, 1, fp1);
        count++;
    }
    fclose(fp1);
    free(temp1);
    free(temp1->name);
}
 
void display(char *a)
{
    FILE *fp1;
    char ch;
    int var = count;
    emp *temp = (emp *)malloc(sizeof(emp));
    temp->name = (char *)malloc(200*sizeof(char));
 
    fp1 = fopen(a, "r"); 
    if (count == 0)
    {
        printf("no records to display\n");
        return;
    }
    if (fp1 == NULL)
        perror("");
    else
    {
        while(var)    
        {
            fread(&temp->empid, sizeof(int), 1, fp1);
            printf("%d", temp->empid);
            fread(temp->name, 200, 1, fp1);
            printf(" %s\n", temp->name);
            var--;
        }
    }
    fclose(fp1);
    free(temp);
    free(temp->name);
}
 
void update(char *a)
{
    FILE *fp1;
    char ch, name[200];
    int var = count, id, c;
    emp *temp = (emp *)malloc(sizeof(emp));
    temp->name = (char *)malloc(200*sizeof(char));
 
    fp1 = fopen(a, "r+");
    if (fp1 == NULL)
        perror("");
    else
    {
        while (var)    
        {
            fread(&temp->empid, sizeof(int), 1, fp1);
            printf("%d", temp->empid);
            fread(temp->name, 200, 1, fp1);
            printf(" %s\n", temp->name);
            var--;
        }
        printf("enter which employee id to be updated\n");
        scanf("%d", &id);
        fseek(fp1, 0, 0);
        var = count;
        while(var)    
        {
            fread(&temp->empid, sizeof(int), 1, fp1);
            if (id == temp->empid)
            {    
                printf("enter employee name for update:");
                scanf(" %[^\n]s", name);
                c = fwrite(name, 200, 1, fp1);    
                break;    
            }
            fread(temp->name, 200, 1, fp1);
            var--;
        } 
        if (c == 1)
            printf("update of the record succesfully\n");
        else
            printf("update unsuccesful enter correct id\n");
            fclose(fp1);
            free(temp);
            free(temp->name);
    }
}

Output
 
Enter the choice
1-Insert a new record into file
2-Display the records
3-Update the record
4-Exit
1
Enter the employee id
100
Enter the employee name
AAA
Enter the choice
1-Insert a new record into file
2-Display the records
3-Update the record
4-Exit
1
Enter the employee id
200
Enter the employee name
BBB
Enter the choice
1-Insert a new record into file
2-Display the records
3-Update the record
4-Exit
1
Enter the employee id
300
Enter the employee name
CCC
Enter the choice
1-Insert a new record into file
2-Display the records
3-Update the record
4-Exit
1
Enter the employee id
400
Enter the employee name
DDD
Enter the choice
1-Insert a new record into file
2-Display the records
3-Update the record
4-Exit
1
Enter the employee id
500
Enter the employee name
EEE
Enter the choice
1-Insert a new record into file
2-Display the records
3-Update the record
4-Exit
2
100 AAA
200 BBB
300 CCC
400 DDD
500 EEE
Enter the choice
1-Insert a new record into file
2-Display the records
3-Update the record
4-Exit
3
100 AAA
200 BBB
300 CCC
400 DDD
500 EEE
enter which employee id to be updated
200
enter employee name for update:CBF
update of the record succesfully
Enter the choice
1-Insert a new record into file
2-Display the records
3-Update the record
4-Exit
2
100 AAA
200 CBF
300 CCC
400 DDD
500 EEE
Enter the choice
1-Insert a new record into file
2-Display the records
3-Update the record
4-Exit
4

No comments:

Post a Comment