fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. // your code goes here
  5. return 0;
  6. }
  7.  
Success #stdin #stdout 0s 5292KB
stdin
#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);
}
stdout
Standard output is empty