Advertisement
Eternoseeker

File handling chinmay

Dec 8th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | Source Code | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. /*Write a program to maintain an employee database in binary file with employee information such as empId,
  5. name, age, department, post and salary. Write function for adding new record, displaying all records,
  6. searching for a particular employee, updating employee salary and post.*/
  7.  
  8. class employee{
  9.     string emp_id;
  10.     string name,department,post;
  11.     string age,salary;
  12. public:
  13.     void input(){
  14.         cout<<"Enter the name of the employee"<<endl;
  15.         cin>>name;
  16.         cout<<"Enter the employee id "<<endl;
  17.         cin>>emp_id;
  18.         cout<<"Enter the age of employee"<<endl;
  19.         cin>>age;
  20.         cout<<"Enter the department for the employee"<<endl;
  21.         cin>>department;
  22.         cout<<"Enter the post for the employee"<<endl;
  23.         cin>>post;
  24.         cout<<"enter the salary of employee"<<endl;
  25.         cin>>salary;
  26.     }
  27.     void display(){
  28.         cout<<"The name of employee is "<<name<<endl;
  29.         cout<<"The employee id is "<<emp_id<<endl;
  30.         cout<<"The age of employee is "<<age<<endl;
  31.         cout<<"The department of employee is "<<department<<endl;
  32.         cout<<"The post for employee is "<<post<<endl;
  33.         cout<<"The salary of employee is "<<salary<<endl;
  34.     }
  35.     void search(int id){
  36.         if(emp_id==id){
  37.             display();
  38.         }
  39.         else{
  40.             cout<<"Data not found"<<endl;
  41.         }
  42.     }
  43.     void update(){
  44.         input();
  45.     }
  46.  
  47. };
  48.  
  49. int main(){
  50.     int ch=0;
  51.     int id;
  52.     fstream file;
  53.     employee e;
  54. do{
  55.     cout<<"\n1.enter the data for employee\n2.display the data for the employee\n3.search the data for employee"<<endl;
  56.     cout<<"4.update the data for the employee\n5.exited"<<endl;
  57.     cout<<"choose any choice"<<endl;
  58.     cin>>ch;
  59.     switch(ch){
  60.     case 1:
  61.         file.open("/home/pccoe/Desktop/employee.txt");
  62.         e.input();
  63.         file.write((char*)&e,sizeof(e));
  64.         file.close();
  65.         break;
  66.     case 2:
  67.         file.open("/home/pccoe/Desktop/employee.txt");
  68.         file.read((char*)&e,sizeof(e));
  69.         e.display();
  70.         file.close();
  71.         break;
  72.     case 3:
  73.         file.open("/home/pccoe/Desktop/employee.txt");
  74.         file.read((char*)&e,sizeof(e));
  75.         cout<<"enter the id of employee to be searched"<<endl;
  76.         cin>>id;
  77.         e.search(id);
  78.         break;
  79.     case 4:
  80.         file.open("/home/pccoe/Desktop/employee.txt",ios::app);
  81.         file.write((char*)&e,sizeof(e));
  82.         e.update();
  83.         break;
  84.     case 5:
  85.         cout<<"file closed"<<endl;
  86.         break;
  87.  
  88.  
  89.  
  90.     }
  91. }while(ch!=5);
  92. return 0;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement