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

C Program To Update Details of Employee Using Files

The program allows the user to update employee records stored in a file by displaying a menu to add, display, or update records, writing the selected option's function which uses file handling functions like fopen(), fwrite(), fread() to add, read, or update the binary file containing the employee struct of id and name fields.

Uploaded by

Ahmad Ali Irfan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
472 views

C Program To Update Details of Employee Using Files

The program allows the user to update employee records stored in a file by displaying a menu to add, display, or update records, writing the selected option's function which uses file handling functions like fopen(), fwrite(), fread() to add, read, or update the binary file containing the employee struct of id and name fields.

Uploaded by

Ahmad Ali Irfan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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. }

Q2
#include <stdio.h>

int main()
{
char line[150];
int i, vowels, consonants, digits, spaces;

vowels = consonants = digits = spaces = 0;

printf("Enter a line of string: ");


scanf("%[^\n]", line);

for(i=0; line[i]!='\0'; ++i)


{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&&
line[i]<='Z'))
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9')
{
++digits;
}
else if (line[i]==' ')
{
++spaces;
}
}

printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);

return 0;
}

You might also like