Advertisement
Eternoseeker

File Handling OOP

Dec 8th, 2022 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | Source Code | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4.  
  5. class emp{
  6. string empid;
  7. string name;
  8.  
  9. public:
  10. emp()
  11. {
  12.     empid = "0";
  13.     name = "abc";
  14. }
  15.  
  16. void accept()
  17. {
  18.     cout<< "Enter employee details" << endl;
  19.     cout << "Enter employee id: " << endl;
  20.     cin >> empid;
  21.     cout << "Enter employee name: " << endl;
  22.     cin >> name;
  23. }
  24.  
  25. void display()
  26. {
  27.     cout << "Employee ID: " << empid << endl;
  28.     cout << "Employee name: " << name << endl;
  29. }
  30. };
  31.  
  32. int main()
  33. {
  34.     emp e1,e2;
  35.     fstream file;
  36.     int ch = 10;
  37.     while(ch != 0){
  38.         cout << "Enter your choices: " << endl;
  39.         cout << "1: Write in file" << endl;
  40.         cout << "2: Read from file" << endl;
  41.         cout << "0: Exit" << endl;
  42.         cin >> ch;
  43.         switch(ch){
  44.             case 1:
  45.             file.open("/workspaces/codespaces-blank/DATA");
  46.             e1.accept();
  47.             file.write((char*)&e1,sizeof(e1));
  48.             file.close();
  49.             break;
  50.             case 2:
  51.             file.open("/workspaces/codespaces-blank/DATA");
  52.             file.read((char*)&e1,sizeof(e1));
  53.             e1.display();
  54.             file.close();
  55.             break;
  56.         }
  57.     }
  58.     return 0;
  59. }
  60.  
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement