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pyZXR1cm47Cn0KRW1wbG95ZWUgZW1wOwpwcmludGYoJnF1b3Q7XG4lLTEwcyAlLTIwcyAlLTIwcyAlLTEwcyAlLTEwc1xuJnF1b3Q7LCAmcXVvdDtFbXAgSUQmcXVvdDssICZxdW90O05hbWUmcXVvdDssICZxdW90O0RlcGFydG1lbnQmcXVvdDssCiZxdW90O1NhbGFyeSZxdW90OywgJnF1b3Q7RXhwZXJpZW5jZSZxdW90Oyk7CnByaW50ZigmcXVvdDstLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tXG4mcXVvdDspOwp3aGlsZSAoZnJlYWQoJmFtcDtlbXAsIHNpemVvZihFbXBsb3llZSksIDEsIGZpbGUpKSB7CnByaW50ZigmcXVvdDslLTEwZCAlLTIwcyAlLTIwcyAlLTEwLjJmICUtMTBkXG4mcXVvdDssIGVtcC5lbXBJRCwgZW1wLmVtcE5hbWUsCmVtcC5kZXBhcnRtZW50LCBlbXAuc2FsYXJ5LCBlbXAueWVhcnNPZkV4cGVyaWVuY2UpOwp9CmZjbG9zZShmaWxlKTsKfQ==
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure to store employee details
typedef struct {
int empID;
char empName[50];
char department[30];
float salary;
int yearsOfExperience;
} Employee;
// Function declarations
void addRecords(const char *filename);
void appendRecord(const char *filename);
void updateRecord(const char *filename);
void deleteRecord(const char *filename);
void displayRecords(const char *filename);
int main() {
const char *filename = "employee_records.dat";
int choice;
do {
printf("\n===== Human Resource Management System =====\n");
printf("1. Add Records\n");
printf("2. Append Record\n");
printf("3. Update Record\n");
printf("4. Delete Record\n");
printf("5. Display Records\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addRecords(filename);
break;
case 2:
appendRecord(filename);
break;
case 3:
updateRecord(filename);
break;
case 4:
deleteRecord(filename);
break;
case 5:
displayRecords(filename);
break;
case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 6);
return 0;
}
void addRecords(const char *filename) {
FILE *file = fopen(filename, "wb");
if (!file) {
perror("Error opening file");
return;
}
int n;
printf("Enter the number of employees to add: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
Employee emp;
printf("Enter employee ID: ");
scanf("%d", &emp.empID);
printf("Enter employee name: ");
scanf(" %[^\n]", emp.empName);
printf("Enter department: ");
scanf(" %[^\n]", emp.department);
printf("Enter salary: ");
scanf("%f", &emp.salary);
printf("Enter years of experience: ");
scanf("%d", &emp.yearsOfExperience);
fwrite(&emp, sizeof(Employee), 1, file);
}
fclose(file);
printf("Records added successfully.\n");
}
void appendRecord(const char *filename) {
FILE *file = fopen(filename, "ab");
if (!file) {
perror("Error opening file");
return;
}
Employee emp;
printf("Enter employee ID: ");
scanf("%d", &emp.empID);
printf("Enter employee name: ");
scanf(" %[^\n]", emp.empName);
printf("Enter department: ");
scanf(" %[^\n]", emp.department);
printf("Enter salary: ");
scanf("%f", &emp.salary);
printf("Enter years of experience: ");
scanf("%d", &emp.yearsOfExperience);
fwrite(&emp, sizeof(Employee), 1, file);
fclose(file);
printf("Record appended successfully.\n");
}
void updateRecord(const char *filename) {
FILE *file = fopen(filename, "rb+");
if (!file) {
perror("Error opening file");
return;
}
int empID;
printf("Enter the employee ID to update: ");
scanf("%d", &empID);
Employee emp;
int found = 0;
while (fread(&emp, sizeof(Employee), 1, file)) {
if (emp.empID == empID) {
printf("Enter new employee name: ");
scanf(" %[^\n]", emp.empName);
printf("Enter new department: ");
scanf(" %[^\n]", emp.department);
printf("Enter new salary: ");
scanf("%f", &emp.salary);
printf("Enter new years of experience: ");
scanf("%d", &emp.yearsOfExperience);
fseek(file, -sizeof(Employee), SEEK_CUR);
fwrite(&emp, sizeof(Employee), 1, file);
found = 1;
break;
}
}
fclose(file);
if (found)
printf("Record updated successfully.\n");
else
printf("Employee ID not found.\n");
}
void deleteRecord(const char *filename) {
FILE *file = fopen(filename, "rb");
if (!file) {
perror("Error opening file");
return;
}
FILE *tempFile = fopen("temp.dat", "wb");
if (!tempFile) {
perror("Error opening temporary file");
fclose(file);
return;
}
int empID;
printf("Enter the employee ID to delete: ");
scanf("%d", &empID);
Employee emp;
int found = 0;
while (fread(&emp, sizeof(Employee), 1, file)) {
if (emp.empID == empID) {
found = 1;
} else {
fwrite(&emp, sizeof(Employee), 1, tempFile);
}
}
fclose(file);
fclose(tempFile);
if (found) {
remove(filename);
rename("temp.dat", filename);
printf("Record deleted successfully.\n");
} else {
remove("temp.dat");
printf("Employee ID not found.\n");
}
}
void displayRecords(const char *filename) {
FILE *file = fopen(filename, "rb");
if (!file) {
perror("Error opening file");
return;
}
Employee emp;
printf("\n%-10s %-20s %-20s %-10s %-10s\n", "Emp ID", "Name", "Department",
"Salary", "Experience");
printf("-------------------------------------------------------------------\n");
while (fread(&emp, sizeof(Employee), 1, file)) {
printf("%-10d %-20s %-20s %-10.2f %-10d\n", emp.empID, emp.empName,
emp.department, emp.salary, emp.yearsOfExperience);
}
fclose(file);
}