-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-16
More file actions
89 lines (78 loc) · 2.2 KB
/
problem-16
File metadata and controls
89 lines (78 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int id;
char name[30];
float salary;
} Employee;
void addEmployee(FILE *fp) {
Employee emp;
printf("Enter Employee ID: ");
scanf("%d", &emp.id);
printf("Enter Employee Name: ");
scanf("%s", emp.name);
printf("Enter Employee Salary: ");
scanf("%f", &emp.salary);
fseek(fp, 0, SEEK_END);
fwrite(&emp, sizeof(Employee), 1, fp);
}
void displayEmployees(FILE *fp) {
Employee emp;
fseek(fp, 0, SEEK_SET);
while (fread(&emp, sizeof(Employee), 1, fp) == 1) {
printf("ID: %d, Name: %s, Salary: %.2f\n", emp.id, emp.name, emp.salary);
}
}
void updateEmployeeSalary(FILE *fp, int id, float newSalary) {
Employee emp;
fseek(fp, 0, SEEK_SET);
while (fread(&emp, sizeof(Employee), 1, fp) == 1) {
if (emp.id == id) {
emp.salary = newSalary;
fseek(fp, -sizeof(Employee), SEEK_CUR);
fwrite(&emp, sizeof(Employee), 1, fp);
break;
}
}
}
int main() {
FILE *fp = fopen("employees.dat", "rb+");
if (fp == NULL) {
fp = fopen("employees.dat", "wb+");
if (fp == NULL) {
perror("Unable to open file");
return 1;
}
}
int choice;
do {
printf("\n1. Add Employee\n2. Display Employees\n3. Update Employee Salary\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addEmployee(fp);
break;
case 2:
displayEmployees(fp);
break;
case 3: {
int id;
float newSalary;
printf("Enter Employee ID to update salary: ");
scanf("%d", &id);
printf("Enter new Salary: ");
scanf("%f", &newSalary);
updateEmployeeSalary(fp, id, newSalary);
break;
}
case 4:
fclose(fp);
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}