0% found this document useful (0 votes)
1 views15 pages

Employee Management System Using File Handling

The document is a C program for an Employee Management System that allows users to add, view, find, edit, and delete employee records stored in a binary file. It defines a structure for employee details and provides functions for each operation, ensuring that employee IDs are unique and records can be managed efficiently. The program runs in a loop until the user chooses to exit, providing a simple text-based interface for interaction.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views15 pages

Employee Management System Using File Handling

The document is a C program for an Employee Management System that allows users to add, view, find, edit, and delete employee records stored in a binary file. It defines a structure for employee details and provides functions for each operation, ensuring that employee IDs are unique and records can be managed efficiently. The program runs in a loop until the user chooses to exit, providing a simple text-based interface for interaction.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <stdio.

h>

#include <stdlib.h>

#include <string.h>

#define FILENAME "[Link]"

struct Employee

int empId;

char name[50];

int age;

char gender[10];

char department[30];

char designation[30];

float salary;

char phone[15];

char email[50];

};

void addEmployee();

void showEmployees();
void findEmployee();

void editEmployee();

void deleteEmployee();

int main()

int choice;

do

printf("\n\n\t====== Employee Management System ======\n");

printf("\n1. Add New Employee");

printf("\n2. View All Employees");

printf("\n3. Find Employee By ID");

printf("\n4. Edit Employee");

printf("\n5. Delete Employee");

printf("\n9. Exit");

printf("\n\nEnter Your Choice : ");

scanf("%d", &choice);

switch (choice)
{

case 1:

addEmployee();

break;

case 2:

showEmployees();

break;

case 3:

findEmployee();

break;

case 4:

editEmployee();

break;

case 5:

deleteEmployee();

break;

case 9:
printf("\nThank You...\n");

exit(0);

default:

printf("\nInvalid Choice...");

} while (1);

return 0;

void addEmployee()

struct Employee emp, temp;

int duplicate = 0;

FILE *fptr = fopen(FILENAME, "rb");

printf("\nEnter Employee ID : ");

scanf("%d", &[Link]);
if (fptr != NULL)

while (fread(&temp, sizeof(struct Employee), 1, fptr))

if ([Link] == [Link])

duplicate = 1;

break;

fclose(fptr);

if (duplicate)

printf("\nEmployee ID Already Exists...");

return;

printf("Enter Name : ");

scanf("%s", [Link]);
printf("Enter Age : ");

scanf("%d", &[Link]);

printf("Enter Gender : ");

scanf("%s", [Link]);

printf("Enter Department : ");

scanf("%s", [Link]);

printf("Enter Designation : ");

scanf("%s", [Link]);

printf("Enter Salary : ");

scanf("%f", &[Link]);

printf("Enter Phone : ");

scanf("%s", [Link]);

printf("Enter Email : ");

scanf("%s", [Link]);

fptr = fopen(FILENAME, "ab");


fwrite(&emp, sizeof(struct Employee), 1, fptr);

fclose(fptr);

printf("\nEmployee Added Successfully...");

void showEmployees()

struct Employee emp;

FILE *fptr = fopen(FILENAME, "rb");

if (fptr == NULL)

printf("\nNo Records Found...");

return;

int count = 1;
printf("\n\n========== ALL EMPLOYEES ==========\n");

while (fread(&emp, sizeof(struct Employee), 1, fptr))

printf("\nEmployee %d", count++);

printf("\n--------------------------------");

printf("\nID : %d", [Link]);

printf("\nName : %s", [Link]);

printf("\nAge : %d", [Link]);

printf("\nGender : %s", [Link]);

printf("\nDepartment : %s", [Link]);

printf("\nDesignation : %s", [Link]);

printf("\nSalary : %.2f", [Link]);

printf("\nPhone : %s", [Link]);

printf("\nEmail : %s", [Link]);

printf("\n--------------------------------\n");

fclose(fptr);

void findEmployee()
{

struct Employee emp;

int id, found = 0;

FILE *fptr = fopen(FILENAME, "rb");

if (fptr == NULL)

printf("\nNo Records Found...");

return;

printf("\nEnter Employee ID : ");

scanf("%d", &id);

while (fread(&emp, sizeof(struct Employee), 1, fptr))

if ([Link] == id)

printf("\n\nEmployee Found");

printf("\n--------------------------------");

printf("\nID : %d", [Link]);


printf("\nName : %s", [Link]);

printf("\nAge : %d", [Link]);

printf("\nGender : %s", [Link]);

printf("\nDepartment : %s", [Link]);

printf("\nDesignation : %s", [Link]);

printf("\nSalary : %.2f", [Link]);

printf("\nPhone : %s", [Link]);

printf("\nEmail : %s", [Link]);

printf("\n--------------------------------");

found = 1;

break;

if (!found)

printf("\nEmployee Not Found...");

fclose(fptr);

void editEmployee()
{

struct Employee emp;

int id, found = 0;

FILE *fptr = fopen(FILENAME, "rb+");

if (fptr == NULL)

printf("\nNo Records Found...");

return;

printf("\nEnter Employee ID To Edit : ");

scanf("%d", &id);

while (fread(&emp, sizeof(struct Employee), 1, fptr))

if ([Link] == id)

printf("\nEnter New Name : ");

scanf("%s", [Link]);
printf("Enter New Age : ");

scanf("%d", &[Link]);

printf("Enter New Gender : ");

scanf("%s", [Link]);

printf("Enter New Department : ");

scanf("%s", [Link]);

printf("Enter New Designation : ");

scanf("%s", [Link]);

printf("Enter New Salary : ");

scanf("%f", &[Link]);

printf("Enter New Phone : ");

scanf("%s", [Link]);

printf("Enter New Email : ");

scanf("%s", [Link]);

fseek(fptr, -sizeof(struct Employee), SEEK_CUR);


fwrite(&emp, sizeof(struct Employee), 1, fptr);

printf("\nRecord Updated Successfully...");

found = 1;

break;

if (!found)

printf("\nEmployee Not Found...");

fclose(fptr);

void deleteEmployee()

struct Employee emp;

int id, found = 0;

FILE *fptr = fopen(FILENAME, "rb");


FILE *temp = fopen("[Link]", "wb");

if (fptr == NULL)

printf("\nNo Records Found...");

return;

printf("\nEnter Employee ID To Delete : ");

scanf("%d", &id);

while (fread(&emp, sizeof(struct Employee), 1, fptr))

if ([Link] == id)

found = 1;

else

fwrite(&emp, sizeof(struct Employee), 1, temp);

}
fclose(fptr);

fclose(temp);

if (!found)

remove("[Link]");

printf("\nEmployee Not Found...");

return;

remove(FILENAME);

rename("[Link]", FILENAME);

printf("\nRecord Deleted Successfully...");

You might also like