0% found this document useful (0 votes)
748 views

C Program To Update Details of Employee Using Files

This C program allows the user to add, display, and update employee records stored in a file. It defines functions to add records by getting employee ID and name input and writing it to a file, display all records by reading the file and printing the data, and update a record by getting an ID, reading the file to find the matching record, and replacing the name.

Uploaded by

Javian Campbell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
748 views

C Program To Update Details of Employee Using Files

This C program allows the user to add, display, and update employee records stored in a file. It defines functions to add records by getting employee ID and name input and writing it to a file, display all records by reading the file and printing the data, and update a record by getting an ID, reading the file to find the matching record, and replacing the name.

Uploaded by

Javian Campbell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C Program to Update Details of Employee using Files

This C Program Updates the Details of Employee using Files.


Here is source code of the C Program to Update Details of Employee using Files. The C
program is successfully compiled and run on a Linux system. The program output is also
shown below.
1. /*
2. * C Program to Update Details of Employee using Files
3. */
4. #include <stdio.h>
5. #include <stdlib.h>
6. #include <string.h>
7. struct emp
8. {
9. int empid;
10. char *name;
11. };
12.
13. int count = 0;
14. void add_rec(char *a);
15. void display(char *a);
16. void update_rec(char *a);
17.
18. void main(int argc, char *argv[])
19. {
20. int choice;
21. while (1)
22. {
23. printf("MENU:\n");
24. printf("1.Add a record\n");
25. printf("2.Display the file\n");
26. printf("3.Update the record\n");
27. printf("Enter your choice:");
28. scanf("%d", &choice);
29.
30. switch(choice)
31. {
32. case 1:
33. add_rec(argv[1]);
34. break;
35. case 2:
36. display(argv[1]);
37. break;
38. case 3:
39. update_rec(argv[1]);
40. break;
41. case 4:
42. exit(0);
43. default:
44. printf("Wrong choice!!!\nEnter the correct choice\n");
45. }
46. }
47. }
48.
49. void add_rec(char *a)
50. {
51. FILE *fp;
52. fp = fopen(a, "a+");
53. struct emp *temp = (struct emp *)malloc(sizeof(struct emp));
54. temp->name = (char *)malloc(50*sizeof(char));
55. if (fp == NULL)
56. printf("Error!!!");
57. else
58. {
59. printf("Enter the employee id\n");
60. scanf("%d", &temp->empid);
61. fwrite(&temp->empid, sizeof(int), 1, fp);
62. printf("enter the employee name\n");
63. scanf(" %[^\n]s", temp->name);
64. fwrite(temp->name, 50, 1, fp);
65. count++;
66. }
67. fclose(fp);
68. free(temp);
69. free(temp->name);
70. }
71.
72. void display(char *a)
73. {
74. FILE *fp;
75. char ch;
76. int rec = count;
77. fp = fopen(a, "r");
78. struct emp *temp = (struct emp *)malloc(sizeof(struct emp));
79. temp->name = (char *)malloc(50*sizeof(char));
80. if (fp == NULL)
81. printf("Error!!");
82. else
83. {
84. while (rec)
85. {
86. fread(&temp->empid, sizeof(int), 1, fp);
87. printf("%d", temp->empid);
88. fread(temp->name, 50, 1, fp);
89. printf(" %s\n", temp->name);
90. rec--;
91. }
92. }
93. fclose(fp);
94. free(temp);
95. free(temp->name);
96. }
97.
98. void update_rec(char *a)
99. {
100. FILE *fp;
101. char ch, name[5];
102. int rec, id, c;
103. fp = fopen(a, "r+");
104. struct emp *temp = (struct emp *)malloc(sizeof(struct emp));
105. temp->name = (char *)malloc(50*sizeof(char));
106. printf("Enter the employee id to update:\n");
107. scanf("%d", &id);
108. fseek(fp, 0, 0);
109. rec = count;
110. while (rec)
111. {
112. fread(&temp->empid, sizeof(int), 1, fp);
113. printf("%d", temp->empid);
114. if (id == temp->empid)
115. {
116. printf("Enter the employee name to be updated");
117. scanf(" %[^\n]s", name);
118. c = fwrite(name, 50, 1, fp);
119. break;
120. }
121. fread(temp->name, 50, 1, fp);
122. rec--;
123. }
124. if (c == 1)
125. printf("Record updated\n");
126. else
127. printf("Update not successful\n");
128. fclose(fp);
129. free(temp);
130. free(temp->name);
131. }
$ cc file5.c
$ a.out empl
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

You might also like