Employee Record System using File Handling in C

This C Program creates employee record and update it.

Here is source code of the C Program to create employee record and update it. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Create Employee Record and Update it
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #define size 200
  8.  
  9. struct emp
  10. {
  11.     int id;
  12.     char *name;
  13. }*emp1, *emp3;
  14.  
  15. void display();
  16. void create();
  17. void update();
  18.  
  19. FILE *fp, *fp1;
  20. int count = 0;
  21.  
  22. void main(int argc, char **argv)
  23. {
  24.     int i, n, ch;
  25.  
  26.     printf("1] Create a Record\n");
  27.     printf("2] Display Records\n");
  28.     printf("3] Update Records\n");
  29.     printf("4] Exit");
  30.     while (1)
  31.     {
  32.         printf("\nEnter your choice : ");
  33.         scanf("%d", &ch);
  34.         switch (ch)
  35.         {
  36.         case 1:    
  37.             fp = fopen(argv[1], "a");
  38.             create();
  39.             break;
  40.         case 2:    
  41.             fp1 = fopen(argv[1],"rb");
  42.             display();
  43.             break;
  44.         case 3:    
  45.             fp1 = fopen(argv[1], "r+");
  46.             update();
  47.             break;
  48.         case 4: 
  49.             exit(0);
  50.         }
  51.     }
  52. }
  53.  
  54. /* To create an employee record */
  55. void create()
  56. {
  57.     int i;
  58.     char *p;
  59.  
  60.     emp1 = (struct emp *)malloc(sizeof(struct emp));
  61.     emp1->name = (char *)malloc((size)*(sizeof(char)));
  62.     printf("Enter name of employee : ");
  63.     scanf(" %[^\n]s", emp1->name);
  64.     printf("Enter emp id : ");
  65.     scanf(" %d", &emp1->id);
  66.     fwrite(&emp1->id, sizeof(emp1->id), 1, fp);
  67.     fwrite(emp1->name, size, 1, fp);
  68.     count++;   // count to number of entries of records
  69.     fclose(fp);
  70. }
  71.  
  72. /* Display the records in the file */
  73. void display()
  74. {    
  75.     emp3=(struct emp *)malloc(1*sizeof(struct emp));    
  76.     emp3->name=(char *)malloc(size*sizeof(char));
  77.     int i = 1;
  78.  
  79.     if (fp1 == NULL)    
  80.         printf("\nFile not opened for reading");
  81.     while (i <= count)
  82.     {
  83.         fread(&emp3->id, sizeof(emp3->id), 1, fp1);
  84.         fread(emp3->name, size, 1, fp1);
  85.         printf("\n%d %s",emp3->id,emp3->name);
  86.         i++;
  87.     }
  88.     fclose(fp1);
  89.     free(emp3->name);
  90.     free(emp3); 
  91. }
  92.  
  93. void update()
  94. {
  95.     int id, flag = 0, i = 1;
  96.     char s[size];
  97.  
  98.     if (fp1 == NULL)
  99.     {
  100.         printf("File cant be opened");
  101.         return;
  102.     }
  103.     printf("Enter employee id to update : ");
  104.     scanf("%d", &id);
  105.     emp3 = (struct emp *)malloc(1*sizeof(struct emp));
  106.         emp3->name=(char *)malloc(size*sizeof(char));
  107.     while(i<=count)
  108.     {    
  109.         fread(&emp3->id, sizeof(emp3->id), 1, fp1);
  110.         fread(emp3->name,size,1,fp1);
  111.         if (id == emp3->id)
  112.         {
  113.             printf("Enter new name of emplyee to update : ");    
  114.             scanf(" %[^\n]s", s);
  115.             fseek(fp1, -204L, SEEK_CUR);
  116.             fwrite(&emp3->id, sizeof(emp3->id), 1, fp1);
  117.             fwrite(s, size, 1, fp1);
  118.             flag = 1;
  119.             break;
  120.         }
  121.         i++;
  122.     }
  123.     if (flag != 1)
  124.     {
  125.         printf("No employee record found");
  126.         flag = 0;
  127.     }
  128.     fclose(fp1);
  129.     free(emp3->name);        /* to free allocated memory */
  130.     free(emp3);
  131. }

$ a.out emprec1
 
1] Create a Record
2] Display Records
3] Update Records
4] Exit
Enter your choice : 1
Enter name of employee : aaa
Enter emp id : 100 
 
Enter your choice : 1
Enter name of employee : bbb
Enter emp id : 200
 
Enter your choice : 1
Enter name of employee : ccc
Enter emp id : 300
 
Enter your choice : 1
Enter name of employee : ddd
Enter emp id : 400
 
Enter your choice : 1
Enter name of employee : eee
Enter emp id : 500 
 
Enter your choice : 2
 
100 aaa
200 bbb
300 ccc
400 ddd
500 eee
Enter your choice : 3
Enter employee id to update : 300
Enter new name of emplyee to update : cdefgh
 
Enter your choice : 2
 
100 aaa
200 bbb
300 cdefgh
400 ddd
500 eee
Enter your choice : 4

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.