complete example creating , updating and deleting a text file in C++

Hello From IQBAL, guys, we often come across situations to store little data in .txt file.
in the code below i have covered: 

  1. creating txt file to store records of employees
  2. showing records from the file(reading file)
  3. updating salary of the employee(updating specific text in the file)
  4. deleting old file when program runs.
  5. An Employee Class with needed Methods
your comments and suggestions are welcomed

#include <iostream>
#include <string>
#include <fstream>
#include <streambuf>

#include <bits/stdc++.h> 

using namespace std;

class Employee{
public:
  void add(string code,string name,int salary) {
 
cout<<"Employee: "<<name<<" with Salary :"<<salary<<" Addedd.";
ofstream file;
  file.open ("employee.txt",std::ios_base::app);
  file <<code<<","<<name<<","<<salary<<";";
  file.close();
}

void incSal(string code,int inc){
string newfile;
fstream fileStream;
fileStream.open("employee.txt");
if (fileStream.fail()) {
    cout<<"Record not found";
} else{
char x;
string id;
string salary;
string name;
int index=0;
while(fileStream>>x){
if(x!=';'){
if(x==','){
index++;
} else
{
switch(index){
case 0: id+=x;break;
case 1: name+=x;break;
case 2: salary+=x;break;
}

}
} else{
if(id.compare(code)==0){
int csal = atoi(salary.c_str());
int newsal = csal+inc;
ostringstream oss;
oss << newsal;
salary = oss.str();
cout<<name<<" Salary updated :"<<salary;

}
newfile+=id+","+name+","+salary+";";
index=0;
id="";
name="";
salary="";
}
}
}
    ofstream file;
                   file.open ("employee.txt");
                   file <<newfile;
                   file.close();
}

void showRec(){
fstream fileStream;
fileStream.open("employee.txt");
if (fileStream.fail()) {
    cout<<"Record not found";
} else{
char x;
while(fileStream>>x){
switch(x){
case ',': cout<<" "; break;
case';': cout<<"\n";break;
default : cout<<x;
}
}
}
}

void removefile(){
  if (remove("employee.txt") == 0) 
      cout<<"old Record Deleted successfully\n"; 
   
}

};


int main(int argc, char** argv) {
Employee emp;
emp.removefile();
string code;
string name;
int salary;
bool run = true;
while(run){
cout<<"Employee Record Management\n";
cout<<"1 : ADD AN EMPLOYEE\n";
cout<<"2 : DSIPLAY FILE RECORDS\n";
cout<<"3 : INCREASE EMPLOYEE SLARAY\n";
int choice;
cout<<"Enter Your choice Number:";

cin>>choice;
switch(choice){
case 1:
cout<<"****Adding employee Record****\n";
cout<<"Enter Code: ";cin>>code;
cout<<"Enter Name: ";cin>>name;
cout<<"Enter Salaray: ";cin>>salary;
emp.add(code,name,salary);
break;
case 2: cout<<"***Employee Record***\n";
emp.showRec();
break;
case 3: 
int inc;
cout<<"Enter Employee Code:";
cin>>code;
cout<<"Enter salary hike:";
cin>>inc;
emp.incSal(code,inc);
break;
default:
cout<<"wrong choice! press y to continue";
}
 cout<<"\n Would you like to continure?(Y/N)"<<endl;
 char ch;
        cin >> ch;
        if(ch =='N'||ch =='n'){
            run = false;
        }
}
return 0;
}


Comments

Popular posts from this blog

Digital Khaata Privacy Policy

how to register latest Wondershare Filmora (version 9.2.1.10 ) with cracked email

INSTALLING WINDOWS FROM A USB