File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
Page 1 of 5
#include<iostream>
#include<iomanip>
#include<cstring>
#include<fstream>
#include<stdlib.h>
using namespace std;
class empclass
{
typedef struct employee
{
int empid;
int salary;
char name[30];
}rec;
typedef struct index
{
int empid;
int position;
}indrec;
rec records;
indrec indrecords;
public:
empclass();
void create();
void display();
void addrec();
void delrec();
void modrec();
};
empclass::empclass()
{
strcpy(records.name,"-");
records.empid=-1;
records.salary=-1;
}
void empclass::create()
{
int i,n;
char ans;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in|ios::out|ios::binary);
indexfile.open("ind.dat",ios::in|ios::out|ios::binary);
indexfile.seekg(0,ios::beg);
seqfile.seekg(0,ios::beg);
cout<<"\nHow many records do you want to enter? ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nID\t: ";
cin>>records.empid;
cout<<"Name\t: ";
cin>>records.name;
cout<<"Salary\t: ";
cin>>records.salary;
seqfile.write((char*)&records,sizeof(records))<<flush;
indrecords.empid=records.empid;
indrecords.position=i;
indexfile.write((char*)&indrecords,sizeof(indrecords))<<flush;
}
seqfile.close();
indexfile.close();
}File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
void empclass::display()
{
int i=0;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in);
indexfile.open("ind.dat",ios::in);
indexfile.seekg(0,ios::beg);
while(indexfile.read((char*)&indrecords,sizeof(indrecords)))
{
i=indrecords.position*sizeof(records);
seqfile.seekg(i,ios::beg);
seqfile.read((char*)&records,sizeof(records));
if((records.empid)!=-1)
{
cout<<"\nID\t: "<<records.empid;
cout<<"\nName\t: "<<records.name;
cout<<"\nSalary\t: "<<records.salary<<endl;
}
}
seqfile.close();
indexfile.close();
}
void empclass::addrec()
{
int i=0;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in|ios::app|ios::out|ios::binary);
indexfile.open("ind.dat",ios::in|ios::app|ios::out|ios::binary);
indexfile.seekg(0,ios::end);
i=indexfile.tellg()/sizeof(indrecords);
cout<<"\nID\t: ";
cin>>records.empid;
cout<<"Name\t: ";
cin>>records.name;
cout<<"Salary\t: ";
cin>>records.salary;
seqfile.write((char*)&records,sizeof(records))<<flush;
indrecords.empid=records.empid;
indrecords.position=i;
indexfile.write((char*)&indrecords,sizeof(indrecords))<<flush;
seqfile.close();
indexfile.close();
}
void empclass::delrec()
{
int id,pos=-1,offset;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in|ios::out|ios::binary);
indexfile.open("ind.dat",ios::in|ios::out|ios::binary);
cout<<"\nEnter ID to delete: ";
cin>>id;
seqfile.seekg(0,ios::beg);
indexfile.seekg(0,ios::beg);
while(indexfile.read((char*)&indrecords,sizeof(indrecords)))
{
if((indrecords.empid)==id)
{
pos=indrecords.position;
break;
}
Page 2 of 5File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
Page 3 of 5
}
if(pos==-1)
cout<<"\nRecord not found!\n";
else
{
offset=pos*sizeof(records);
seqfile.seekg(offset,ios::beg);
records.empid=-1;
records.salary=-1;
strcpy(records.name,"-");
seqfile.write((char*)&records,sizeof(records))<<flush;
indrecords.empid=-1;
indrecords.position=pos;
offset=pos*sizeof(indrecords);
indexfile.seekg(offset,ios::beg);
indexfile.write((char*)&indrecords,sizeof(indrecords))<<flush;
cout<<"Record Deleted!\n";
}
seqfile.close();
indexfile.close();
}
void empclass::modrec()
{
int id,pos=-1,offset=-1;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in|ios::out|ios::binary);
indexfile.open("ind.dat",ios::in|ios::out|ios::binary);
cout<<"\nEnter ID to modify: ";
cin>>id;
seqfile.seekg(0,ios::beg);
indexfile.seekg(0,ios::beg);
while(indexfile.read((char*)&indrecords,sizeof(indrecords)))
{
if((indrecords.empid)==id)
{
pos=indrecords.position;
break;
}
}
if(pos==-1)
cout<<"\nRecord not found!\n";
else
{
offset=pos*sizeof(records);
seqfile.seekg(offset,ios::beg);
cout<<"\nName\t: ";
cin>>records.name;
cout<<"Salary\t: ";
cin>>records.salary;
records.empid=id;
seqfile.write((char*)&records,sizeof(records))<<flush;
indrecords.empid=id;
indrecords.position=pos;
offset=pos*sizeof(indrecords);
indexfile.seekg(offset,ios::beg);
indexfile.write((char*)&indrecords,sizeof(indrecords))<<flush;
cout<<"Record Modified!\n";
}
seqfile.close();
indexfile.close();
}
int main()
{File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
empclass obj;
int ch;
while(1)
{
cout<<"\n\tMENU";
cout<<"\n1.Create Database";
cout<<"\n2.Display Database";
cout<<"\n3.Add record";
cout<<"\n4.Delete a record";
cout<<"\n5.Modify a record";
cout<<"\n6.Exit";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1: obj.create();
break;
case 2: obj.display();
break;
case 3: obj.addrec();
break;
case 4: obj.delrec();
break;
case 5: obj.modrec();
break;
case 6: exit(0);
}
}
return(0);
}
/*
OUTPUT
saiesh@saiesh :~$ g++ ads10.cpp
saiesh@saiesh :~$ ./a.out
MENU
1.Create Database
2.Display Database
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 1
How many records do you want to enter? 4
ID
Name
Salary : 1
: Saiesh
: 10000
ID
Name
Salary : 2
: Rohan
: 8000
ID
Name
Salary : 3
: Misba
: 7500
ID
Name
Salary : 4
: Payal
: 7000
MENU
1.Create Database
2.Display Database
Page 4 of 5File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 2
ID
Name
Salary : 1
: Saiesh
: 10000
ID
Name
Salary : 2
: Rohan
: 8000
ID
Name
Salary : 3
: Misba
: 7500
ID
Name
Salary : 4
: Payal
: 7000
MENU
1.Create Database
2.Display Database
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 3
ID
Name
Salary
: 5
: Karishma
: 6000
MENU
1.Create Database
2.Display Database
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 4
Enter ID to delete: 4
Record Deleted!
MENU
1.Create Database
2.Display Database
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 5
Enter ID to modify: 5
Name
: Karishma
Salary : 7000
Record Modified!
saiesh@saiesh :~$ */
Page 5 of 5
Page 1 of 5
#include<iostream>
#include<iomanip>
#include<cstring>
#include<fstream>
#include<stdlib.h>
using namespace std;
class empclass
{
typedef struct employee
{
int empid;
int salary;
char name[30];
}rec;
typedef struct index
{
int empid;
int position;
}indrec;
rec records;
indrec indrecords;
public:
empclass();
void create();
void display();
void addrec();
void delrec();
void modrec();
};
empclass::empclass()
{
strcpy(records.name,"-");
records.empid=-1;
records.salary=-1;
}
void empclass::create()
{
int i,n;
char ans;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in|ios::out|ios::binary);
indexfile.open("ind.dat",ios::in|ios::out|ios::binary);
indexfile.seekg(0,ios::beg);
seqfile.seekg(0,ios::beg);
cout<<"\nHow many records do you want to enter? ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nID\t: ";
cin>>records.empid;
cout<<"Name\t: ";
cin>>records.name;
cout<<"Salary\t: ";
cin>>records.salary;
seqfile.write((char*)&records,sizeof(records))<<flush;
indrecords.empid=records.empid;
indrecords.position=i;
indexfile.write((char*)&indrecords,sizeof(indrecords))<<flush;
}
seqfile.close();
indexfile.close();
}File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
void empclass::display()
{
int i=0;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in);
indexfile.open("ind.dat",ios::in);
indexfile.seekg(0,ios::beg);
while(indexfile.read((char*)&indrecords,sizeof(indrecords)))
{
i=indrecords.position*sizeof(records);
seqfile.seekg(i,ios::beg);
seqfile.read((char*)&records,sizeof(records));
if((records.empid)!=-1)
{
cout<<"\nID\t: "<<records.empid;
cout<<"\nName\t: "<<records.name;
cout<<"\nSalary\t: "<<records.salary<<endl;
}
}
seqfile.close();
indexfile.close();
}
void empclass::addrec()
{
int i=0;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in|ios::app|ios::out|ios::binary);
indexfile.open("ind.dat",ios::in|ios::app|ios::out|ios::binary);
indexfile.seekg(0,ios::end);
i=indexfile.tellg()/sizeof(indrecords);
cout<<"\nID\t: ";
cin>>records.empid;
cout<<"Name\t: ";
cin>>records.name;
cout<<"Salary\t: ";
cin>>records.salary;
seqfile.write((char*)&records,sizeof(records))<<flush;
indrecords.empid=records.empid;
indrecords.position=i;
indexfile.write((char*)&indrecords,sizeof(indrecords))<<flush;
seqfile.close();
indexfile.close();
}
void empclass::delrec()
{
int id,pos=-1,offset;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in|ios::out|ios::binary);
indexfile.open("ind.dat",ios::in|ios::out|ios::binary);
cout<<"\nEnter ID to delete: ";
cin>>id;
seqfile.seekg(0,ios::beg);
indexfile.seekg(0,ios::beg);
while(indexfile.read((char*)&indrecords,sizeof(indrecords)))
{
if((indrecords.empid)==id)
{
pos=indrecords.position;
break;
}
Page 2 of 5File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
Page 3 of 5
}
if(pos==-1)
cout<<"\nRecord not found!\n";
else
{
offset=pos*sizeof(records);
seqfile.seekg(offset,ios::beg);
records.empid=-1;
records.salary=-1;
strcpy(records.name,"-");
seqfile.write((char*)&records,sizeof(records))<<flush;
indrecords.empid=-1;
indrecords.position=pos;
offset=pos*sizeof(indrecords);
indexfile.seekg(offset,ios::beg);
indexfile.write((char*)&indrecords,sizeof(indrecords))<<flush;
cout<<"Record Deleted!\n";
}
seqfile.close();
indexfile.close();
}
void empclass::modrec()
{
int id,pos=-1,offset=-1;
fstream seqfile;
fstream indexfile;
seqfile.open("emp.dat",ios::in|ios::out|ios::binary);
indexfile.open("ind.dat",ios::in|ios::out|ios::binary);
cout<<"\nEnter ID to modify: ";
cin>>id;
seqfile.seekg(0,ios::beg);
indexfile.seekg(0,ios::beg);
while(indexfile.read((char*)&indrecords,sizeof(indrecords)))
{
if((indrecords.empid)==id)
{
pos=indrecords.position;
break;
}
}
if(pos==-1)
cout<<"\nRecord not found!\n";
else
{
offset=pos*sizeof(records);
seqfile.seekg(offset,ios::beg);
cout<<"\nName\t: ";
cin>>records.name;
cout<<"Salary\t: ";
cin>>records.salary;
records.empid=id;
seqfile.write((char*)&records,sizeof(records))<<flush;
indrecords.empid=id;
indrecords.position=pos;
offset=pos*sizeof(indrecords);
indexfile.seekg(offset,ios::beg);
indexfile.write((char*)&indrecords,sizeof(indrecords))<<flush;
cout<<"Record Modified!\n";
}
seqfile.close();
indexfile.close();
}
int main()
{File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
empclass obj;
int ch;
while(1)
{
cout<<"\n\tMENU";
cout<<"\n1.Create Database";
cout<<"\n2.Display Database";
cout<<"\n3.Add record";
cout<<"\n4.Delete a record";
cout<<"\n5.Modify a record";
cout<<"\n6.Exit";
cout<<"\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1: obj.create();
break;
case 2: obj.display();
break;
case 3: obj.addrec();
break;
case 4: obj.delrec();
break;
case 5: obj.modrec();
break;
case 6: exit(0);
}
}
return(0);
}
/*
OUTPUT
saiesh@saiesh :~$ g++ ads10.cpp
saiesh@saiesh :~$ ./a.out
MENU
1.Create Database
2.Display Database
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 1
How many records do you want to enter? 4
ID
Name
Salary : 1
: Saiesh
: 10000
ID
Name
Salary : 2
: Rohan
: 8000
ID
Name
Salary : 3
: Misba
: 7500
ID
Name
Salary : 4
: Payal
: 7000
MENU
1.Create Database
2.Display Database
Page 4 of 5File: /home/saiesh/ADS/Assignment No 10/ads10.cpp
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 2
ID
Name
Salary : 1
: Saiesh
: 10000
ID
Name
Salary : 2
: Rohan
: 8000
ID
Name
Salary : 3
: Misba
: 7500
ID
Name
Salary : 4
: Payal
: 7000
MENU
1.Create Database
2.Display Database
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 3
ID
Name
Salary
: 5
: Karishma
: 6000
MENU
1.Create Database
2.Display Database
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 4
Enter ID to delete: 4
Record Deleted!
MENU
1.Create Database
2.Display Database
3.Add record
4.Delete a record
5.Modify a record
6.Exit
Enter your choice: 5
Enter ID to modify: 5
Name
: Karishma
Salary : 7000
Record Modified!
saiesh@saiesh :~$ */
Page 5 of 5
Comments
Post a Comment