Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- using namespace std;
- class emp{
- string 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;
- }
- string 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], e2[4], e3[3], e4[4];
- fstream file, file2;
- string deleteid;
- string updateid;
- string searchid;
- 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 << "5: Update data in file"<<endl;
- cout << "0: Exit" << endl;
- cin >> ch;
- switch(ch){
- case 1:
- file.open("/home/pccoe/Desktop/empdata.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/empdata.txt");
- for(int i = 0; i < 4; i++){
- file.read((char*)&e2[i], sizeof(e2[i]));
- e2[i].display();
- }
- file.close();
- break;
- case 3:
- cout << "Enter the id to search: " << endl;
- cin >> searchid;
- file.open("/home/pccoe/Desktop/empdata.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:
- cout << "Enter the id to delete: " << endl;
- cin >> deleteid;
- file2.open("/home/pccoe/Desktop/empdatanew.txt");
- for(int i = 0; i < 4; i++){
- if(e1[i].retid() != deleteid){
- file2.write((char*)&e1[i], sizeof(e1[i]));
- }
- }
- file2.close();
- file2.open("/home/pccoe/Desktop/empdatanew.txt");
- for(int i=0;i<3;i++){
- file2.read((char*)&e3[i],sizeof(e3[i]));
- cout<<e3[i].retname()<<endl;
- }
- file2.close();
- //remove("/home/pccoe/Desktop/emp.txt");
- //rename("/home/pccoe/Desktop/empdatanew.txt", "/home/pccoe/Desktop/empdata.txt");
- break;
- case 5:
- int pos;
- cout << "Enter the id to update: " << endl;
- cin >> updateid;
- file.open("/home/pccoe/Desktop/empdata.txt");
- for(int i = 0; i < 4; i++)
- {
- pos=file.tellg();
- file.read((char*)&e2[i], sizeof(e2[i]));
- if(e2[i].retid() == updateid)
- {
- e2[i].accept();
- flag=1;
- }
- file.close();
- }
- cout<<pos;
- if(flag == 1){
- file.open("/home/pccoe/Desktop/empdata.txt");
- for(int i = 0;i < 4; i++){
- file.seekp(pos);
- file.write((char*)&e2[i],sizeof(e2[i]));
- }
- file.close();
- }
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement