Write a C program for Employee Record System: The main features of this project
ID: 3712750 • Letter: W
Question
Write a C program for Employee Record System:
The main features of this project include basic file handling operations; you are to add, list, modify and delete data to/from file.
Implement following functions for employees
Add record
List record
Modify record
Delete record
You are to prompt the user for list of operations and execute the selected function until user selects to exit. Use data structures and file handling to store the records.
Sample main menu:
Sample run of add records:
Notes:
1- Code should be fully commented and structured (using functions).
2- You can only use the following in completing the program:
a- stdio.h: printf,scanf,fprintf,fscanf,gets,puts,fputs and fgets.
b-selection statements.
c-loops.
d-functions
e-pointers.
f-files.
g-strings.
1. Add Record 2. List Records 3. Modify Records 4 Delete Records 5. Exit Your Choice:Explanation / Answer
#include<stdio.h>
struct employee
{
int empid;
char name[30];
float salary;
}emp;
// FUNCTION TO INSERT RECORDS TO THE FILE
void insert()
{
FILE *fp;
fp = fopen("Record", "a");
printf("Enter the emp no :");
scanf("%d", &emp.empid);
printf("Enter the Name :");
scanf("%s", &emp.name);
printf("Enter the salary :");
scanf("%f", &emp.salary);
fwrite(&emp, sizeof(emp), 1, fp);
fclose(fp);
}
// FUNCTION TO DISPLAY RECORDS
void disp()
{
FILE *fp1;
fp1 = fopen("Record", "r");
printf(" emp Number Name salary ");
while (fread(&emp, sizeof(emp), 1, fp1))
printf(" %d %s %.2f ", emp.empid, emp.name, emp.salary);
fclose(fp1);
}
// FUNCTION TO DELETE A RECORD
void deletefile()
{
FILE *fpo;
FILE *fpt;
int r, s;
printf("Enter the emp no you want to delete :");
scanf("%d", &r);
if (avlempid(r) == 0)
printf("emp no %d is not available in the file ", r);
else
{
fpo = fopen("Record", "r");
fpt = fopen("TempFile", "w");
while (fread(&emp, sizeof(emp), 1, fpo))
{
s = emp.empid;
if (s != r)
fwrite(&emp, sizeof(emp), 1, fpt);
}
fclose(fpo);
fclose(fpt);
fpo = fopen("Record", "w");
fpt = fopen("TempFile", "r");
while (fread(&emp, sizeof(emp), 1, fpt))
fwrite(&emp, sizeof(emp), 1, fpo);
printf(" RECORD DELETED ");
fclose(fpo);
fclose(fpt);
}
}
// FUNCTION TO UPDATE THE RECORD
void update()
{
int avl;
FILE *fpt;
FILE *fpo;
int s, r, ch;
printf("Enter emp number to update:");
scanf("%d", &r);
avl = avlempid(r);
if (avl == 0)
{
printf("emp number %d is not Available in the file", r);
}
else
{
fpo = fopen("Record", "r");
fpt = fopen("TempFile", "w");
while (fread(&emp, sizeof(emp), 1, fpo))
{
s = emp.empid;
if (s != r)
fwrite(&emp, sizeof(emp), 1, fpt);
else
{
printf(" 1. Update Name of emp Number %d", r);
printf(" 2. Update salary of emp Number %d", r);
printf(" 3. Update both Name and salary of emp Number %d", r);
printf(" Enter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter Name:");
scanf("%s", &emp.name);
break;
case 2:
printf("Enter salary : ");
scanf("%f", &emp.salary);
break;
case 3:
printf("Enter Name: ");
scanf("%s", &emp.name);
printf("Enter salary: ");
scanf("%f", &emp.salary);
break;
default:
printf("Invalid Selection");
break;
}
fwrite(&emp, sizeof(emp), 1, fpt);
}
}
fclose(fpo);
fclose(fpt);
fpo = fopen("Record", "w");
fpt = fopen("TempFile", "r");
while (fread(&emp, sizeof(emp), 1, fpt))
{
fwrite(&emp, sizeof(emp), 1, fpo);
}
fclose(fpo);
fclose(fpt);
printf("RECORD UPDATED");
}
}
// FUNCTION TO CHECK GIVEN emp NO IS AVAILABLE //
int avlempid(int rno)
{
FILE *fp;
int c = 0;
fp = fopen("Record", "r");
while (!feof(fp))
{
fread(&emp, sizeof(emp), 1, fp);
if (rno == emp.empid)
{
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
//FUNCTION TO CHECK THE FILE IS EMPTY OR NOT
int empty()
{
int c = 0;
FILE *fp;
fp = fopen("Record", "r");
while (fread(&emp, sizeof(emp), 1, fp))
c = 1;
fclose(fp);
return c;
}
// MAIN PROGRAM
void main()
{
int c, emp;
do
{
printf(" ---Select your choice--------- ");
printf(" 1. ADD RECORDS 2. LIST RECORDS");
printf(" 3. DELETE RECORDS 4. MODIFY RECORDS");
printf(" 5. EXIT");
printf(" ------------------------------------------ ");
printf(" Enter your choice:");
scanf("%d", &c);
printf(" ");
switch (c)
{
case 1:
insert();
break;
case 2:
emp = empty();
if (emp == 0)
printf(" The file is EMPTY ");
else
disp();
break;
case 3:
deletefile();
break;
case 4:
update();
break;
case 5:
exit(1);
break;
default:
printf(" Your choice is wrong Please try again... ");
break;
}
} while (c != 7);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.