In a small firm employee numbers are given in serial numerical order, that is 1, 2, 3, etc.

C
Sections
13
Exercises

A

B

C

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

- Create a file of employee data with the following information: employee number, name, sex, gross salary.
- If more employees join, append their data to the file.
- If an employee with serial number 25 (say) leaves, delete the record by making gross salary 0.
- If some employee’s gross salary increases, retrieve the record and update the salary.

Write a program to implement the above operations.

``````#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>

struct empo
{
int eid;
char name[20];
char sex;
int salary;
};

int main()
{
addemp(1, "Kishor Dass", 'M', 100);
addemp(2, "Amit Kumar Mandal", 'M', 200);
addemp(3, "Prakash Babu Sharma", 'M', 300);
addemp(4, "Mohd Seraj Alam", 'M', 400);
addemp(5, "Priya Saxena", 'F', 500);
addemp(6, "Aabha Chaudhary", 'F', 600);
addemp(7, "Varun Tanjea", 'M', 700);
addemp(8, "Ankit Bhardwaj", 'M', 800);
change_sal(1, 1000);
disemp();
_getch();
return 0;
}

void addemp(int eid, char *name, char sex, int salary)
{
struct empo help;
FILE *fp;
fp = fopen("Emp.txt", "rb+");
if (fp == NULL)
fp = fopen("Emp.txt", "wb");
fseek(fp, 0, SEEK_END);
help.eid = eid;
strcpy(help.name, name);
help.sex = sex;
help.salary = salary;
fwrite(&help, sizeof(help), 1, fp);
fclose(fp);
}

void change_sal(int id, int new_salary)
{
struct empo help;
int size = sizeof(help);
FILE *fp;
fp = fopen("Emp.txt", "rb+");
if (fp == NULL)
fp = fopen("Emp.txt", "wb");
while (fread(&help, sizeof(help), 1, fp) == 1)
{
if (help.eid == id)
{
fseek(fp, -size, SEEK_CUR);
help.salary = new_salary;
fwrite(&help, sizeof(help), 1, fp);
break;
}
}
fclose(fp);

}

void delemp(int id)
{
struct empo help;
int size = sizeof(help);
FILE *fp;
fp = fopen("Emp.txt", "rb+");
if (fp == NULL)
fp = fopen("Emp.txt", "wb");
while (fread(&help, sizeof(help), 1, fp) == 1)
{
if (help.eid == id)
{
fseek(fp, -size, SEEK_CUR);
help.salary = 0;
fwrite(&help, sizeof(help), 1, fp);
break;
}
}
fclose(fp);
}

void disemp()
{
struct empo help;
FILE *fp;
fp = fopen("Emp.txt", "rb");
if (fp == NULL)
{
puts("\nFile is unable to open or did not exist.\nExiting. . .");
exit(1);
}
while (fread(&help, sizeof(help), 1, fp) == 1)
{
printf("\n%2d", help.eid);
printf("\t%-20s", help.name);
printf("\t%c", help.sex);
printf("\t%d\n", help.salary);
}
fclose(fp);
}``````

© 2020 Garbage Value