0% found this document useful (0 votes)
6 views

Practical 8

Uploaded by

Saurabh Asnare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Practical 8

Uploaded by

Saurabh Asnare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical 8

Name: Saurabh Asnare (SYCOA 276)


#include <iostream>
#include<string> cout<<"\n\nEnter Employee
Post :";
#include<fstream>
cin>>post;
}
using namespace std;

void display(){
class Employee{
int emp_id,age;
cout<<"\n"<<emp_id<<"\
char name[30],department[30],post[30];
t"<<name<<"\t"<<salery<<"\
float salery; t"<<department<<"\t"<<post;

public: }
int getEmpId(){

void read(){ return emp_id;


}

cout<<"\n\nEnter Employee Id :";


cin>>emp_id; };
void writeInFile(){

cout<<"\n\nEnter Employee Employee e;


Name :";
cin>>name;
ofstream ofs;
e.read(); //accept data from user
cout<<"\n\nEnter Employee Salary
ofs.open("emp.dat",ios::binary|ios::app);
:";
ofs.write((char*)&e,sizeof(e));//write
cin>>salery;
data into the file
ofs.close();
cout<<"\n\nEnter Employee
}
Department:";
cin>>department;
void readFromFile(){
Employee e; void updateFile( int id){

ifstream ifs; int pos, flag = 0;


ifs.open("emp.dat",ios::binary);
fstream fs;
while(ifs.read((char*)&e,sizeof(e))){ Employee e;
e.display(); fs.open("emp.dat",ios::in | ios::binary |
ios::out);
}
ifs.close();
while (!fs.eof()) {
}
// storing the position of current file
pointer i.e. at the end of previously read
void search( int id){ record

ifstream fin; pos = fs.tellg();

int flag=0;
fin.open("emp.dat",ios::binary); fs.read((char*)&e, sizeof(e));
if (fs) {

Employee e;
if (id == e.getEmpId()) {

while(fin.read((char *)&e,sizeof(e))){ flag = 1;

if(id==e.getEmpId()){
cout<<"\n\nEmployee Id e.read();
found..";
flag=1;
// placing the put(writing)
e.display(); pointer at the starting of the record

} fs.seekp(pos);

}
if(flag==0){ fs.write((char*)&e, sizeof(e));

cout<<"\nEmployee Not Found....";


} e.display();

fin.close(); break;

} }
} cout<<"\
n--------------------------------------------";
}
readFromFile();
fs.close();
break;

if (flag == 1)
case 3:
cout << "\nRecord successfully
modified \n"; cout<<"\n\nEnter Employee
Id to Find Employee:";
else
cin>>id;
cout << "\nRecord not found \n";
cout<<"\nEmpId\
tEmpName\tSalary\tDept\tPost";
}
cout<<"\
int main() n--------------------------------------------";

{ search(id);

int ch,id; break;

while(1){
cout<<"\nSelect from below case 4:
choices";
cout<<"\nEnter employee
cout<<"\n1.Insert Data \n2.Display \ id for updateFile:";
n3.Search\n4.Update Reord\n5.Exit";
cin>>id;
cout<<"\nEnter Your Choice:";
updateFile(id);
cin>>ch;
break;
case 5:
switch(ch){
exit(0);
break;
case 1:
}
writeInFile();
break;
}
return 0;
case 2:
}
cout<<"\nEmpId\
Output
tEmpName\tSalary\tDept\tPost";
Select from below choices
1.Insert Data EmpId EmpName Salary Dept Post
2.Display --------------------------------------------
3.Search 101 sau 100 comp hod
4.Update Reord Select from below choices
5.Exit 1.Insert Data
Enter Your Choice:1 2.Display
3.Search
4.Update Reord
Enter Employee Id :101 5.Exit
Enter Your Choice:1

Enter Employee Name :sau


Enter Employee Id :102

Enter Employee Salary :100


Enter Employee Name :om

Enter Employee Department:comp


Enter Employee Salary :2000

Enter Employee Post :hod


Enter Employee Department:it
Select from below choices
1.Insert Data
2.Display Enter Employee Post :devloper
3.Search
4.Update Reord Select from below choices
5.Exit 1.Insert Data
Enter Your Choice:2 2.Display
3.Search
4.Update Reord
5.Exit
Enter Your Choice:3 Enter Employee Department:comp

Enter Employee Id to Find Employee:101 Enter Employee Post :hod

EmpId EmpName Salary Dept Post 101 saurabh 4000 comp hod
-------------------------------------------- Record successfully modified

Employee Id found..
101 sau 100 comp hod
Select from below choices
1.Insert Data
2.Display
3.Search
4.Update Reord
5.Exit
Enter Your Choice:4

Enter employee id for updateFile:101

Enter Employee Id :101

Enter Employee Name :saurabh

Enter Employee Salary :4000

You might also like