Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include<fstream>
- using namespace std;
- class emp{
- int empid;
- string name;
- public:
- emp()
- {
- empid = 0;
- name = "abc";
- }
- void accept()
- {
- cout<< "Enter employee details" << endl;
- cout << "Enter employee id: " << endl;
- cin >> empid;
- cout << "Enter employee name: " << endl;
- cin >> name;
- }
- void display()
- {
- cout << "Employee ID: " << empid << endl;
- cout << "Employee name: " << name << endl;
- }
- int retid(){
- return empid;
- }
- string retname(){
- return name;
- }
- /*void search(int id){
- if(id == empid){
- display();
- }
- else{
- cout<<"Data not found"<<endl;
- }
- }
- */
- };
- int main()
- {
- emp e1[4];
- emp e2[4],e3[3];
- fstream file,file2;
- int ch = 10;
- int flag = 0;
- while(ch != 0){
- cout << "Enter your choices: " << endl;
- cout << "1: Write in file" << endl;
- cout << "2: Read from file" << endl;
- cout << "3: Search in file" << endl;
- cout << "4: Delete data in file" << endl;
- cout << "0: Exit" << endl;
- cin >> ch;
- switch(ch){
- case 1:
- file.open("/home/pccoe/Desktop/emp.txt");
- for(int i = 0; i < 4; i++){
- e1[i].accept();
- file.write((char*)&e1[i],sizeof(e1[i]));
- }
- file.close();
- break;
- case 2:
- file.open("/home/pccoe/Desktop/emp.txt");
- for(int i = 0; i < 4; i++){
- file.read((char*)&e2[i],sizeof(e2[i]));
- e2[i].display();
- }
- file.close();
- break;
- case 3:
- int searchid;
- cout << "Enter the id to search: " << endl;
- cin >> searchid;
- file.open("/home/pccoe/Desktop/emp.txt");
- for(int i = 0; i < 4; i++){
- file.read((char*)&e2[i], sizeof(e2[i]));
- if(e2[i].retid() == searchid){
- cout << "Data found" << endl;
- cout << e2[i].retname() << endl;
- flag = 1;
- }
- }
- if(flag == 0){
- cout << "data not found" << endl;
- }
- file.close();
- break;
- case 4:
- int delete_id;
- cout << "Enter the id to delete: " << endl;
- cin >> delete_id;
- file2.open("/home/pccoe/Desktop/cpy.txt");
- for(int i = 0; i < 4; i++){
- if(e1[i].retid() != delete_id){
- file2.write((char*)&e1[i], sizeof(e1[i]));
- }
- }
- file2.close();
- file2.open("/home/pccoe/Desktop/cpy.txt");
- for(int i=0;i<3;i++)
- {
- file2.read((char*)&e3[i],sizeof(e3[i]));
- cout<<e3[i].retname()<<endl;
- }
- // remove("/home/pccoe/Desktop/emp.txt");
- rename("cpy.txt", "emp.txt");
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement