/* Copyright (C) 2007 Ankur Banerjee. This program is free software: you can redistribute it and / or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For a copy of the GNU General Public License see http://www.gnu.org/licenses/gpl.html /
/ Binary Files – Employee Data Management Program. Give user the option to create & delete file, show number of records, delete particular record, and delete
last record /
#include <fstream.h>
#include <conio.h>
#include <stdio.h>
class employee
{
int eno;
char ename[20];
double salary;
public: void getdata()
{
cout<<endl<<“Enter employee number : “;
cin>>eno;
cout<<“Enter employee name : “;
gets(ename);
cout<<“Enter employee salary : “;
cin>>salary;
}
void disp()
{
cout<<endl<<Displaying data for employee”
<<endl<<“Employee number : “<<eno
<<endl<<“Employee name : “<<ename
<<endl<<“Employee salary : “<<salary<<endl;
}
} obj1, obj2;
char ans;
fstream file, temp;
int ch, r;
void create()
{
file.open(“data”, ios::out | ios::binary);
do
{
obj1.getdata();
file.write((char)&obj1, sizeof(obj1));
cout<<“Do you wish to enter another record? (y / n): “;
cin>>ans;
} while (ans == ‘y’ || ans == ‘Y’);
file.close();
}
void showfile()
{
file.open(“data”, ios::in);
while (file.read((char)&obj1, sizeof(obj1)))
obj1.disp();
file.close();
}
void countrec()
{
r = 0; file.open(“data”, ios::in);
while (file.read((char)&obj1, sizeof(obj1)))
++r;
file.close();
cout<<endl<<“Number of records is “<<r<<endl;
}
void rewrite()
{
temp.open(“new”, ios::in);
file.open(“data”, ios::out);
temp.seekg(0);
while (temp.read((char)&obj2, sizeof(obj2)))
file.write((char)&obj2, sizeof(obj2));
file.close();
temp.close();
remove(“new”);
cout<<“\nRecord you requested to be removed has been deleted\n”;
}
void delrec()
{
file.open(“data”, ios::in);
temp.open(“new”, ios::out);
r = 1;
int rec;
cout<<endl<<“Enter record number to delete : “;
cin>>rec; file.seekg(0);
while (file.read((char)&obj2, sizeof(obj2)))
{
if (r != rec)
temp.write((char)&obj2, sizeof(obj2));
++r;
}
file.close();
temp.close();
rewrite();
}
void dellast()
{
file.open(“data”, ios::in);
temp.open(“new”, ios::out);
file.seekg(0, ios::end); // go to end of file
int lp = file.tellg();
int s = sizeof(obj1);
int lr = lp / s;
r = 1;
file.seekg(0);
while (file.read((char)&obj2, sizeof(obj2)))
{
if (r != lr)
temp.write((char)&ob
j2, sizeof(obj2));
++r;
}
file.close();
temp.close();
rewrite();
}
void main()
{
clrscr();
do
{
cout<<endl<<“Employee Data Management Program” <<endl<<“1. Create file”
<<endl<<“2. Read file”
<<endl<<“3. Find total number of records”
<<endl<<“4. Delete nth record”
<<endl<<“5. Delete the last record”
<<endl<<“Enter your choice : “;
cin>>ch;
switch(ch)
{
case 1 : create();
break;
case 2 : showfile();
break;
case 3 : countrec();
break;
case 4 : delrec();
break;
case 5 : dellast();
break;
default : cout<<“\nYou entered an incorrect choice”;
}
cout<<“\nDo you want the menu to be shown again? (y / n): “;
cin>>ans;
} while (ans == ‘y’ || ans == ‘Y’ );
getch();
}
/* Output /
Employee Data Management Program
1. Create file
2. Read file
3. Find total number of records
4. Delete nth record
5. Delete the last record
Enter your choice : 1
Enter employee number : 1
Enter employee name : Arthur Dent
Enter employee salary : 45000
Do you wish to enter another record? (y / n): y
Enter employee number : 2
Enter employee name : Tricia McMillian
Enter employee salary : 12000
Do you wish to enter another record? (y / n): y
Enter employee number : 3
Enter employee name : Ford Prefect
Enter employee salary : 50000
Do you wish to enter another record? (y / n): y
Enter employee number : 4
Enter employee name : Zaphod Beeblebrox
Enter employee salary : 30000
Do you wish to enter another record? (y / n): y
Enter employee number : 5
Enter employee name : Zarniwoop
Enter employee salary : 6000
Do you wish to enter another record? (y / n): n
Do you want the menu to be shown again? (y / n) : y
Employee Data Management Program
1. Create file
2. Read file
3. Find total number of records
4. Delete nth record
5. Delete the last record
Enter your choice : 3
Number of records is 5
Do you want the menu to be shown again? (y / n) : y
Employee Data Management Program
1. Create file
2. Read file
3. Find total number of records
4. Delete nth record
5. Delete the last record
Enter your choice : 5
Record you requested to be removed has been deleted
Do you want the menu to be shown again? (y / n) : y
Employee Data Management Program
1. Create file
2. Read file
3. Find total number of records
4. Delete nth record
5. Delete the last record
Enter your choice : 2
Displaying data for employee
Employee number : 1
Employee name : Arthur Dent
Employee salary : 45000
Displaying data for employee
Employee number : 2
Employee name : Tricia McMillian
Employee salary : 12000
Displaying data for employee
Employee number : 3
Employee name : Ford Prefect
Employee salary : 50000
Displaying data for
employee
Employee number : 4
Employee name : Zaphod Beeblebrox
Employee salary : 30000
Do you want the menu to be shown again? (y / n) : y
Employee Data Management Program
1. Create file
2. Read file
3. Find total number of records
4. Delete nth record
5. Delete the last record
Enter your choice : 4
Enter record number to delete : 2
Record you requested to be removed has been deleted
Do you want the menu to be shown again? (y / n) : y
Employee Data Management Program
1. Create file
2. Read file
3. Find total number of records
4. Delete nth record
5. Delete the last record
Enter your choice : 2
Displaying data for employee
Employee number : 1
Employee name : Arthur Dent
Employee salary : 45000
Displaying data for employee
Employee number : 3
Employee name : Ford Prefect
Employee salary : 50000
Displaying data for employee
Employee number : 4
Employee name : Zaphod Beeblebrox
Employee salary : 30000
Do you want the menu to be shown again? (y / n) : n
/ Program faults: It does not check whether record number to be deleted exceeds available number of records or not. Can be easily checked by taking value from countrec() and putting a check in delrec() */