📜  使用文件处理的C员工记录系统

📅  最后修改于: 2021-05-28 03:52:10             🧑  作者: Mango

员工记录系统是为处理公司的主要内务管理功能而构建的软件。 ERS帮助公司跟踪所有员工及其记录。它用于通过计算机系统来管理公司。该软件可处理任何公司的员工记录。它将帮助公司跟踪文件中所有员工的记录。

员工记录系统的目标:将为用户提供5个选项:

  • 添加新记录。
  • 删除记录。
  • 修改记录。
  • 查看所有记录。
  • 出口。

员工资料:

  • 名称。
  • 年龄。
  • 薪水。
  • 员工ID。

方法:所有功能将在开关盒下提供。想法是使用文件处理的概念将数据写入文本文件并读取已写入的数据。我们还需要在同一文件夹中添加一个data.txt文件。

方法:

  • 开头的框架由应用程序和开发人员的名称组成:它是使用一些printf语句和一个名为system()的预定义函数创建的。 system()函数是C / C++标准库的一部分。它用于传递可以在命令处理器或操作系统终端中执行的命令,并在命令完成后最终返回该命令。
  • 系统(“颜色3F”)将更改控制台的颜色,即背景(3)和控制台上的文本,即前景(F)。
  • 系统(“ pause” )将暂停屏幕,因此用户将收到一条消息:按任意键继续。 。 。
  • gotoxy()函数:将有助于设置显示数据的坐标。
  • 开关盒开关盒下的所需函数将根据用户的输入执行。使用简单的文件处理概念(例如打开文件,关闭文件,写入文件和读取文件等)来开发代码。

下面是Employee记录系统的C程序:

C
// C program for  the above approach
#include 
#include 
#include 
#include 
  
// 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;
}


输出:

  • 首先显示软件名称:
  • 显示所有选项:
  • 添加记录:
  • 显示记录:
  • 删除记录:
  • 删除后记录:
  • 修改或编辑记录:
  • 修改后记录:

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。