Maintain Employee Records using File Handling
Write a C Program to Maintain Employee Records using File Handling. Here’s simple Program to Maintain Employee Records using File Handling in C Programming Language.
Below is the source code for C Program to Maintain Employee Records using File Handling which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C Program to Maintain Employee Records using File Handling */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define size 200 struct emp { int id; char *name; }*emp1, *emp3; void display(); void create(); void update(); char * arrr = "C:\\Users\\acer\\Documents\\file4.txt"; FILE *fp, *fp1; int count = 0; void main() { int i, n, ch; printf("1] Create a Record\n"); printf("2] Display Records\n"); printf("3] Update Records\n"); printf("4] Exit"); while (1) { printf("\nEnter your choice : "); scanf("%d", &ch); switch (ch) { case 1: fp = fopen(arrr, "a"); create(); break; case 2: fp1 = fopen(arrr,"rb"); display(); break; case 3: fp1 = fopen(arrr, "r+"); update(); break; case 4: exit(0); } } } /* To create an employee record */ void create() { int i; char *p; emp1 = (struct emp *)malloc(sizeof(struct emp)); emp1->name = (char *)malloc((size)*(sizeof(char))); printf("Enter name of employee : "); scanf(" %[^\n]s", emp1->name); printf("Enter emp id : "); scanf(" %d", &emp1->id); fwrite(&emp1->id, sizeof(emp1->id), 1, fp); fwrite(emp1->name, size, 1, fp); count++; // count to number of entries of records fclose(fp); } /* Display the records in the file */ void display() { emp3=(struct emp *)malloc(1*sizeof(struct emp)); emp3->name=(char *)malloc(size*sizeof(char)); int i = 1; if (fp1 == NULL) printf("\nFile not opened for reading"); while (i <= count) { fread(&emp3->id, sizeof(emp3->id), 1, fp1); fread(emp3->name, size, 1, fp1); printf("\n%d %s",emp3->id,emp3->name); i++; } fclose(fp1); free(emp3->name); free(emp3); } void update() { int id, flag = 0, i = 1; char s[size]; if (fp1 == NULL) { printf("File cant be opened"); return; } printf("Enter employee id to update : "); scanf("%d", &id); emp3 = (struct emp *)malloc(1*sizeof(struct emp)); emp3->name=(char *)malloc(size*sizeof(char)); while(i<=count) { fread(&emp3->id, sizeof(emp3->id), 1, fp1); fread(emp3->name,size,1,fp1); if (id == emp3->id) { printf("Enter new name of emplyee to update : "); scanf(" %[^\n]s", s); fseek(fp1, -204L, SEEK_CUR); fwrite(&emp3->id, sizeof(emp3->id), 1, fp1); fwrite(s, size, 1, fp1); flag = 1; break; } i++; } if (flag != 1) { printf("No employee record found"); flag = 0; } fclose(fp1); free(emp3->name); /* to free allocated memory */ free(emp3); }
OUTPUT : :
1] Create a Record 2] Display Records 3] Update Records 4] Exit Enter your choice : 1 Enter name of employee : codezclub Enter emp id : 1 Enter your choice : 1 Enter name of employee : Max Enter emp id : 2 Enter your choice : 1 Enter name of employee : John Enter emp id : 3 Enter your choice : 2 1 codezclub 2 Max 3 John Enter your choice : 3 Enter employee id to update : 2 Enter new name of emplyee to update : Max Payne Enter your choice : 2 1 codezclub 2 Max Payne 3 John Enter your choice : 4 Process returned 0
Above is the source code for C Program to Maintain Employee Records using File Handling which is successfully compiled and run on Windows System.The Output of the program is shown above .
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post….
Are there any linked lists in this program?