After learning all the operations in bits, let's now try to combine all the concepts to create a complete program. The first example we will are going to make is an "Employee Management Program". This program will give the user the option to "add,display,search and delete an employee". All the operations are performed using different functions. For example, to add a record we have used the "add_emp()" function and to display the list of employees the "show_emp()" function has been used. Since this is the first example of managing records, I've tried to keep it as simple as possible. Read the code carefully and then read the description to understand how the code works.
Example 1 : Basic employee management program using binary file handling functions.
#include
#include
#include
struct employee
{
int empno,salary;
char ename[15];
float tax,netsal;
}
// Public section
employee emp;
FILE *fptr;
void main()
{
clrscr();
// Function Prototyping
void add_emp(); // to add a new emp
void show_emp(); // to display all emp
void search_emp(); // to search an emp
void del_emp(); // to delete an emp
char c;
do
{
// Main menu
printf("\na. Add New Employee Information");
printf("\nb. Show all employees");
printf("\nc. Search an employee");
printf("\nd. Delete employee data");
printf("\ne. Exit");
fflush(stdin);
printf("\nSelect an option (a/b/c/d/e) : ");
scanf("%c",&c);
if(c=='a' || c=='A')
add_emp();
else if(c=='b' || c=='B')
show_emp();
else if(c=='c' || c=='C')
search_emp();
else if(c=='d' || c=='D')
del_emp();
}while(c!='e' && c!='E');
fclose(fptr);
}
void add_emp()
{
clrscr();
fptr=fopen("emp_data","a"); // opens the file in append mode
printf("\nAdd Employee");
printf("\n-+-+-+-+-+-+-+-+-");
char c;
do
{
printf("\nEnter employee no. : ");
scanf("%d",&emp.empno);
printf("\nEnter employee name : ");
scanf("%s",&emp.ename);
printf("\nEnter basic salary : ");
scanf("%d",&emp.salary);
// Calculating tax and net salary
emp.tax=emp.salary*3/100;
emp.netsal=emp.salary-emp.tax;
fwrite(&emp,sizeof(employee),1,fptr); // writing the data to the file
fflush(stdin);
printf("\nNew Record Added");
printf("\nWould you like to add more records (y/n) : ");
scanf("%c",&c);
clrscr();
}while(c=='y');
fclose(fptr);
}
void show_emp()
{
clrscr();
fptr=fopen("emp_data","r");
while(fread(&emp,sizeof(employee),1,fptr)==1) // reading the file till tne end
{
printf("\nEmployee number %d",emp.empno);
printf("\nEmployee name %s",emp.ename);
printf("\nBasic Salary %d",emp.salary);
printf("\nIncome tax %.2f",emp.tax);
printf("\nNet Salary (After deducting tax) : %.2f",emp.netsal); // %.2f will show only 2 decimal points
printf("\n===++++====++++====++++=====+++++"); // just a seperator (optional)
}
fclose(fptr);
}
void search_emp()
{
clrscr();
int choice,eno,flag;
char enm[12];
fptr=fopen("emp_data","r");
// asking the user to select a choice
printf("\nSearch employee");
printf("\n1. Search by empno");
printf("\n2. Search by name");
printf("\nSelect a criteria (1/2) : ");
scanf("%d",&choice);
if(choice==1)// if searching is by empno
{
clrscr();
printf("\nSearch by empno ");
printf("\n==================");
printf("\nEnter empno to search : ");
scanf("%d",&;eno);
while(fread(&emp,sizeof(employee),1,fptr)==1) // reading the records
{
if(emp.empno==eno)// comparing the empno with the value entered by the user
{
printf("\nEmployee Data Found");
printf("\n-------------------");
printf("\nEmpno %d",emp.empno);
printf("\nEmp name %s",emp.ename);
printf("\nSalary %d",emp.salary);
printf("\nIncome tax %.2f",emp.tax);
printf("\nNet Salary %.2f",emp.netsal);
flag=1; // marking the presence of the record
}
}
if(flag!=1)
printf("\nEmpno %d not found",eno);
}
else if(choice==2) // searching by name
{
clrscr();
printf("\nSearch by emp name ");
printf("\n==================");
printf("\nEnter emp name to search : ");
scanf("%s",&enm);
while(fread(&emp,sizeof(employee),1,fptr)==1)
{
if(strcmp(emp.ename,enm)==0) // comparing the names using strcmp
{
printf("\nEmployee Data Found");
printf("\n-------------------");
printf("\nEmpno %d",emp.empno);
printf("\nEmp name %s",emp.ename);
printf("\nSalary %d",emp.salary);
printf("\nIncome tax %.2f",emp.tax);
printf("\nNet Salary %.2f",emp.netsal);
flag=1;
}
}
if(flag!=1)
printf("\nEmp name %s not found",enm);
}
printf("\nPress any key to continue..");
getch();
clrscr();
}
void del_emp()
{
clrscr();
int choice,eno,flag;
char enm[12];
FILE *fptr2;
fptr2=fopen("temp","w"); // creating a temp file in writing mode
fptr=fopen("emp_data","r");
printf("\nSearch employee");
printf("\n1. Delete by empno");
printf("\n2. Delete by name");
printf("\nSelect a criteria (1/2) : ");
scanf("%d",&choice);
if(choice==1)
{
clrscr();
printf("\nDelete by empno ");
printf("\n==================");
printf("\nEnter empno to delete : ");
scanf("%d",&eno);
while(fread(&emp,sizeof(employee),1,fptr)==1)
{
if(emp.empno!=eno) // copying all the records in the temp file except the one to be deleted
{
fwrite(&emp,sizeof(emp),1,fptr2);
}
}
}
else if(choice==2)
{
clrscr();
printf("\nDelete by emp name ");
printf("\n==================");
printf("\nEnter emp name to delete : ");
scanf("%s",&enm);
while(fread(&emp,sizeof(employee),1,fptr)==1)
{
if(strcmp(emp.ename,enm)!=0)
{
fwrite(&emp,sizeof(emp),1,fptr2);
}
}
}
printf("\nRecord Deleted");
printf("\nPress any key to continue..");
fclose(fptr);
fclose(fptr2);
remove("emp_data");//deleting the original file
rename("temp","emp_data");// renaming the file to restore the original file
getch();
clrscr();
}
Description (Function wise)
Public Section (The area just below the inclusion of header files)
main()
add_data()
show_data()
search_data()
To search a specific record, the user has two options - by empno or by name. If the user opts to search by empno by entering "1" when asked, the program will ask the user to enter the empno which is to be searched. Suppose the empno entered are "1,2,3,4,5" and the user wants to delete the record of empno "3", the user will enter "3" to delete this record. The "while" loop will start reading the records from the beginning till the end of the file. The first record read will be "1", the if condition(if emp.empno==eno) will result in false and the loop will proceed to read the second record. This time the comparison will be between the empno of the second employee(2) and 3, since the condition is false again the loop will continue to read the next record. The next record is of empno "3" which is the record that we want to search, the "if" condition will become true, and the details of the 3rd employee will printed on the screen.
If the user enters "2" to search by name, the same process happens but comparing the names this time. The name entered by the user will be compared against all the names inside the file. The moment the name read from the file matches the name entered by the user the program will display all the details of the employee.
del_data()
Like "search_data()" the user is again given two choices - delete by empno or by name. Before deleting the specified record we must learn how the data is going to be deleted. We can not delete the record directly from the original file, instead of this we will copy all the records from the original file, except the record that we want to delete, to a temporary file. For example, if the user wants to delete the record of empno "5", all the other records(1,2,3,4) will be copied to a temporary file. Now the other file contains all the records except the one which was to be deleted. After the copy is over, the original file will be deleted, and the temporary file will be renamed to the name of the original file.
Take a look at the code, after asking the user which record to delete, the loop copies those records which are not equal to the record that is to be deleted "if(emp.empno!=eno)". Once all the records are copied to the temporary file, both the files are closed and the original file("emp_data") is deleted. With the deletion of this file all the records are also deleted. Since all the other functions are using the name of the original file and we have deleted it, it will create the program to show errors because the file does not exists any more. Therefore to complete the deletion process we have renamed the temporary file ("temp") to the original file name ("emp_data").
0 comments:
Post a Comment