C Program to Generate Employee Filename Records Extracted from Command Line Arguments

This C Program creates employee file name record that is taken from the command line argument.

Here is source code of the C Program to create employee file name record that is taken from the command line argument. 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 File Name Record that is taken from the Command-Line Argument 
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. #include <string.h>
  8.  
  9. struct emprec
  10. {
  11.     int empid;
  12.     char *name;
  13. };
  14. typedef struct emprec emp;
  15.  
  16. void insert(char *a);
  17. void display(char *a);
  18. void update(char *a);
  19. int count;
  20. void main(int argc, char *argv[])
  21. {
  22.     int choice;    
  23.  
  24.     while (1)
  25.     {
  26.         printf("Enter the choice\n");
  27.         printf("1-Insert a new record into file\n2-Display the records\n");
  28.         printf("3-Update the record\n4-Exit\n");
  29.         scanf("%d",  &choice);
  30.         switch (choice)
  31.         {
  32.         case 1:
  33.             insert(argv[1]);
  34.             break;
  35.         case 2:
  36.             display(argv[1]);
  37.             break;
  38.         case 3:
  39.             update(argv[1]);
  40.             break;
  41.         case 4:
  42.             exit(0);
  43.         default:
  44.             printf("Enter the correct choice\n");
  45.         }
  46.     } 
  47. }
  48.  
  49. /* To insert a new recored into the file */
  50. void insert(char *a)
  51. {
  52.     FILE *fp1;
  53.     emp *temp1 = (emp *)malloc(sizeof(emp));
  54.     temp1->name = (char *)malloc(200 * sizeof(char)); //allocating memory for pointer 
  55.  
  56.     fp1 = fopen(a, "a+");
  57.     if (fp1 == NULL)
  58.         perror("");
  59.     else
  60.     {
  61.         printf("Enter the employee id\n");
  62.         scanf("%d", &temp1->empid);
  63.         fwrite(&temp1->empid, sizeof(int), 1, fp1);
  64.         printf("Enter the employee name\n");
  65.         scanf(" %[^\n]s", temp1->name);
  66.         fwrite(temp1->name, 200, 1, fp1);
  67.         count++;
  68.     }
  69.     fclose(fp1);
  70.     free(temp1);
  71.     free(temp1->name);
  72. }
  73.  
  74. /* To display the records in the file */
  75. void display(char *a)
  76. {
  77.     FILE *fp1;
  78.     char ch;
  79.     int var = count;
  80.     emp *temp = (emp *)malloc(sizeof(emp));
  81.     temp->name = (char *)malloc(200*sizeof(char));
  82.  
  83.     fp1 = fopen(a, "r"); 
  84.     if (count == 0)
  85.     {
  86.         printf("no records to display\n");
  87.         return;
  88.     }
  89.     if (fp1 == NULL)
  90.         perror("");
  91.     else
  92.     {
  93.         while(var)    // display the employee records
  94.         {
  95.             fread(&temp->empid, sizeof(int), 1, fp1);
  96.             printf("%d", temp->empid);
  97.             fread(temp->name, 200, 1, fp1);
  98.             printf(" %s\n", temp->name);
  99.             var--;
  100.         }
  101.     }
  102.     fclose(fp1);
  103.     free(temp);
  104.     free(temp->name);
  105. }
  106.  
  107. /* To Update the given record */
  108. void update(char *a)
  109. {
  110.     FILE *fp1;
  111.     char ch, name[200];
  112.     int var = count, id, c;
  113.     emp *temp = (emp *)malloc(sizeof(emp));
  114.     temp->name = (char *)malloc(200*sizeof(char));
  115.  
  116.     fp1 = fopen(a, "r+");
  117.     if (fp1 == NULL)
  118.         perror("");
  119.     else
  120.     {
  121.         while (var)    //displaying employee records so that user enter correct employee id
  122.         {
  123.             fread(&temp->empid, sizeof(int), 1, fp1);
  124.             printf("%d", temp->empid);
  125.             fread(temp->name, 200, 1, fp1);
  126.             printf(" %s\n", temp->name);
  127.             var--;
  128.         }
  129.         printf("enter which employee id to be updated\n");
  130.         scanf("%d", &id);
  131.         fseek(fp1, 0, 0);
  132.         var = count;
  133.         while(var)    //loop to update the name of entered employeeid
  134.         {
  135.             fread(&temp->empid, sizeof(int), 1, fp1);
  136.             if (id == temp->empid)
  137.             {    
  138.                 printf("enter employee name for update:");
  139.                 scanf(" %[^\n]s", name);
  140.                 c = fwrite(name, 200, 1, fp1);    
  141.                 break;    
  142.             }
  143.             fread(temp->name, 200, 1, fp1);
  144.             var--;
  145.         } 
  146.         if (c == 1)
  147.             printf("update of the record succesfully\n");
  148.         else
  149.             printf("update unsuccesful enter correct id\n");
  150.             fclose(fp1);
  151.             free(temp);
  152.             free(temp->name);
  153.     }
  154. }

$ cc file2.c
$ a.out emp
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

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.

If you find any mistake above, kindly email to [email protected]

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.