Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Employee Record System in C using File Handling

  • Difficulty Level : Medium
  • Last Updated : 12 Nov, 2021

Employee Record System is software built to handle the primary housekeeping functions of a company. ERS helps companies keep track of all the employees and their records. It is used to manage the company using a computerized system. This software built to handle the records of employees of any company. It will help companies to keep track of all the employees’ records in a file.

Aim of the Employee’s Record System: The user will be provided with 5 options:

We provide nothing but the best curated videos and practice problems for our students. Check out the C Foundation Course and master the C language from basic to advanced level. Wait no more, start learning today!
  • Add a new record.
  • Delete a record.
  • Modify a record.
  • View all the records.
  • Exit.

Data of the Employees:

  • Name.
  • Age.
  • Salary.
  • Employee ID.

Approach: All the functions will be provided under switch cases. The idea is to use the concepts of File Handling to write the data in a text file and read the written data as well. We need to add a data.txt file in the same folder as well.



Approach:

  • The opening frame consists of the name of the application and the developer: It is created using some printf statements and a predefined function called system(). The system() function is a part of the C/C++ standard library. It is used to pass the commands that can be executed in the command processor or the terminal of the operating system and finally returns the command after it has been completed.
  • system(“Color 3F”) will change the color of the console i.e. background (3)  and the text on the console i.e. foreground (F).
  • system(“pause”) will pause the screen, so the user will get a message: Press any key to continue . . .
  • gotoxy() function: It will help to set the coordinates of the displayed data.
  • Switch Case: The required function under the switch cases will be executed as per the input of the user. Simple file handling concepts like opening a file, closing a file, writing in a file, and reading the file, etc. are used to develop the code.

Below is the C program for the Employee record system:

C




// C program for  the above approach
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
  
// Structure of the employee
struct emp {
    char name[50];
    float salary;
    int age;
    int id;
};
struct emp e;
  
// size of the structure
long int size = sizeof(e);
  
// In the start coordinates
// will be 0, 0
COORD cord = { 0, 0 };
  
// function to set the
// coordinates
void gotoxy(int x, int y)
{
    cord.X = x;
    cord.Y = y;
    SetConsoleCursorPosition(
        GetStdHandle(STD_OUTPUT_HANDLE),
        cord);
}
  
FILE *fp, *ft;
  
// Function to add the records
void addrecord()
{
    system("cls");
    fseek(fp, 0, SEEK_END);
    char another = 'y';
  
    while (another == 'y') {
        printf("\nEnter Name : ");
        scanf("%s", e.name);
  
        printf("\nEnter Age : ");
        scanf("%d", &e.age);
  
        printf("\nEnter Salary : ");
        scanf("%f", &e.salary);
  
        printf("\nEnter EMP-ID : ");
        scanf("%d", &e.id);
  
        fwrite(&e, size, 1, fp);
  
        printf("\nWant to add another"
               " record (Y/N) : ");
        fflush(stdin);
  
        scanf("%c", &another);
    }
}
  
// Function to delete the records
void deleterecord()
{
    system("cls");
    char empname[50];
    char another = 'y';
  
    while (another == 'y') {
        printf("\nEnter employee "
               "name to delete : ");
        scanf("%s", empname);
  
        ft = fopen("temp.txt", "wb");
        rewind(fp);
  
        while (fread(&e, size,
                     1, fp)
               == 1) {
            if (strcmp(e.name,
                       empname)
                != 0)
                fwrite(&e, size, 1, ft);
        }
  
        fclose(fp);
        fclose(ft);
        remove("data.txt");
        rename("temp.txt", "data.txt");
        fp = fopen("data.txt", "rb+");
  
        printf("\nWant to delete another"
               " record (Y/N) :");
        fflush(stdin);
        another = getche();
    }
}
  
// Function to display the record
void displayrecord()
{
    system("cls");
  
    // sets pointer to start
    // of the file
    rewind(fp);
  
    printf("\n========================="
           "==========================="
           "======");
    printf("\nNAME\t\tAGE\t\tSALARY\t\t"
           "\tID\n",
           e.name, e.age,
           e.salary, e.id);
    printf("==========================="
           "==========================="
           "====\n");
  
    while (fread(&e, size, 1, fp) == 1)
        printf("\n%s\t\t%d\t\t%.2f\t%10d",
               e.name, e.age, e.salary, e.id);
  
    printf("\n\n\n\t");
    system("pause");
}
  
// Function to modify the record
void modifyrecord()
{
    system("cls");
    char empname[50];
    char another = 'y';
  
    while (another == 'y') {
        printf("\nEnter employee name"
               " to modify : ");
        scanf("%s", empname);
  
        rewind(fp);
  
        // While File is open
        while (fread(&e, size, 1, fp) == 1) {
            // Compare the employee name
            // with ename
            if (strcmp(e.name, empname) == 0) {
                printf("\nEnter new name:");
                scanf("%s", e.name);
                printf("\nEnter new age :");
                scanf("%d", &e.age);
                printf("\nEnter new salary :");
                scanf("%f", &e.salary);
                printf("\nEnter new EMP-ID :");
                scanf("%d", &e.id);
  
                fseek(fp, -size, SEEK_CUR);
                fwrite(&e, size, 1, fp);
                break;
            }
        }
  
        // Ask for modifying another record
        printf("\nWant to modify another"
               " record (Y/N) :");
        fflush(stdin);
        scanf("%c", &another);
    }
}
  
// Driver code
int main()
{
    int choice;
  
    // opening the file
    fp = fopen("data.txt", "rb+");
  
    // showing error if file is
    // unable to open.
    if (fp == NULL) {
        fp = fopen("data.txt", "wb+");
        if (fp == NULL) {
            printf("\nCannot open file...");
            exit(1);
        }
    }
  
    system("Color 3F");
    printf("\n\n\n\n\t\t\t\t============="
           "============================="
           "===========");
    printf("\n\t\t\t\t~~~~~~~~~~~~~~~~~~~"
           "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
           "~~~~~");
    printf("\n\t\t\t\t==================="
           "============================="
           "=====");
    printf("\n\t\t\t\t[|:::>:::>:::>::>  "
           "EMPLOYEE RECORD  <::<:::<:::"
           "<:::|]\t");
    printf("\n\t\t\t\t==================="
           "============================="
           "=====");
    printf("\n\t\t\t\t~~~~~~~~~~~~~~~~~~~~"
           "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
           "~~~");
    printf("\n\t\t\t\t====================="
           "==============================\n");
    printf("\n\n\n\t\t\t\t\t\t\t\t\t\t"
           "Developer : @Sushant_Gaurav"
           "\n\n\t\t\t\t");
  
    system("pause");
  
    while (1) {
        // Clearing console and asking the
        // user for input
        system("cls");
        gotoxy(30, 10);
        printf("\n1. ADD RECORD\n");
        gotoxy(30, 12);
        printf("\n2. DELETE RECORD\n");
        gotoxy(30, 14);
        printf("\n3. DISPLAY RECORDS\n");
        gotoxy(30, 16);
        printf("\n4. MODIFY RECORD\n");
        gotoxy(30, 18);
        printf("\n5. EXIT\n");
        gotoxy(30, 20);
        printf("\nENTER YOUR CHOICE...\n");
        fflush(stdin);
        scanf("%d", &choice);
  
        // Switch Case
        switch (choice) {
        case 1:
  
            // Add the records
            addrecord();
            break;
  
        case 2:
  
            // Delete the records
            deleterecord();
            break;
  
        case 3:
  
            // Display the records
            displayrecord();
            break;
  
        case 4:
  
            // Modify the records
            modifyrecord();
            break;
  
        case 5:
            fclose(fp);
            exit(0);
            break;
  
        default:
            printf("\nINVALID CHOICE...\n");
        }
    }
  
    return 0;
}

Output:

  • First displaying the name of software:
  • Displaying all the options:
  • Adding Records:
  • Displaying Records:
  • Delete a Record:

  • Record After Deletion:
  • Modifying or Editing a record:
  • Record after Modification:



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!