1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
#include<fstream>
#include<iostream>
using namespace std;
class employee
{
private:
int id;
float salary;
char name[9];
public:
int getid() //returns the id assigned to an employee.
{
return id;
}
void setdata(int ID) //inputs the data about employees.
{
id = ID;
cout<<"enter the name\n";
cin>>name;
cout<<"enter the salary\n";
cin>>salary;
}
void display() //displays the data
{
cout<<"\t"<<id;
cout<<"\t\t"<<name;
cout<<"\t\t"<<salary;
cout<<"\n";
}
};
void add_employee(); /* adds the new records by opening the file "DataFile.dat" with append mode.*/
void edit_employee();/* edits the existing records by opening the file "DataFile.dat" with ate mode.*/
void delete_employee(); /*deletes the existing records by opening the file "DataFile.dat" with out mode.*/
void display_employee();/*display the data about an employee.*/
void display_all();/*displays the data about all the employees*/
int getID();/* returns the unique id for new employees. */
int sizeoffile();/*returns the size of DataFile.dat in terms of number of employees*/
char file[] = {"DataFile.dat"}; //variable having the name of master file.
char metafile[] = {"metadata.dat"};//varable having the name of file that stores the unique id of record that is recently added.
int main()
{
int ch;
while(1)
{
cout<<"\t\tEmployee Management System\n";
cout<<"===========================================\n\n";
cout<<"1:\tDisplay Records of all Employees\n";
cout<<"2:\tAdd New Employee\n";
cout<<"3:\tDelete Employee Record\n";
cout<<"4:\tEdit Eployee Record\n";
cout<<"5:\tGet Information About An Employee\n";
cout<<"6:\tExit\n";
cout<<"===========================================\n\n";
cout<<"\tEnter Your Choice...\n> ";
cin>>ch;
switch(ch)
{
case 1:
display_all();
cout<<endl;
system("pause");
system("cls");
break;
case 2:
add_employee();
cout<<endl;
system("pause");
system("cls");
break;
case 3:
delete_employee();
cout<<endl;
system("pause");
system("cls");
break;
case 4:
edit_employee();
cout<<endl;
system("pause");
system("cls");
break;
case 5:
display_employee();
cout<<endl;
system("pause");
system("cls");
break;
case 6:
exit(0);
break;
default:
cout<<"\nInvalid Input\n";
system("pause");
system("cls");
}
}
return(0);
}
int getID() //returns the unique id for new records.
{
ifstream fin(metafile);
int value = 0;
if(fin == 0)
{
fin.close();
ofstream fout(metafile); //this block is executed only for first time when the program is run.
fout<<0;
fout.close();
fin.open(metafile);
}
fin>>value; //reads the unique id which is recently assigned to a record.
fin.close();
ofstream fout(metafile);
fout<<value+1; //writes the value of id after increamenting it by one.
fout.close();
return (value+1);
}/*all the above function code reads the value from file then increament it and write back to the file
as well as returns the value to calling statement.*/
void add_employee()
{
employee e1;
fstream fout(file,ios::app|ios::out);
system("cls");
e1.setdata(getID()); //calling get id function to get unique id for new record.
fout.seekp(0,ios::end);
fout.write((char *)&e1,sizeof(e1));
cout<<"File Updated Successfuly...\n";
fout.close();
}
void edit_employee() //this function is having problem with sizeoffile function.
{
employee temp;
int id,counter=0;
int size = sizeoffile();
system("cls");
cout<<"\tEnter the Employee ID\n>\t";
cin>>id;
fstream fout(file,ios::ate|ios::out|ios::in);
cout<<"\n"<<sizeoffile()<<"\n"; /*here the function sizeoffile() is returning
expected value(ie.no of records in file DataFile.dat*/
if(sizeoffile() == 0) /*here the function sizeoffile() is returning 0(zero)...?*/
{
cout<<"\nNo Record found...\n";
fout.close();
return;
}
else
{
for(int i=0;i<size;i++) //for loop that traverses all the records in the file DataFile.dat.
{
fout.read((char *)&temp,sizeof(temp));
if(id == temp.getid()) //id == temp.getid() means record that we are looking for is found.
{
int size = sizeoffile();
size = size - sizeof(temp);
fout.seekp(size,ios::beg);
temp.setdata(id); //updating the record.
fout.write((char *)&temp,sizeof(temp));
counter = 1;
break;
}
}
}
fout.close();
if(counter==0)
cout<<"\nRecord Not Found...\n";
else
cout<<"\nRecord updated Successfuly...\n";
}
void delete_employee()
{
employee e1;
int id,counter=0;
system("cls");
cout<<"\nEnter the Employee ID...\n>";
cin>>id;
ifstream fin(file);
ofstream fout("temp.dat");
int size = sizeoffile();
if(sizeoffile() == 0)
{
cout<<"\nNo Record found...\n";
fin.close();
return;
}
else
{
fin.seekg(0,ios::beg);
for(int i=0;i<size;i++) /*for loop copies all the records(excluding one that is to be deleted)
from DataFile.dat to temp.dat*/
{
fin.read((char *)&e1,sizeof(e1));
if(e1.getid()==id)
{
counter = 1;
continue;
}
fout.write((char *)&e1,sizeof(e1));
}
}
fin.close();
fout.close();
if(counter == 0)
{
cout<<"\nRecord Not Found...\n";
}
else
{
fout.open(file);
fin.open("temp.dat");
fin.seekg(0,ios::beg);
for(int i = 0;i<size-1;i++) /*for loop overwriting the contents stored in DataFile.dat
with th contents of temp.dat file.*/
{
fin.read((char *)&e1,sizeof(e1));
fout.write((char *)&e1,sizeof(e1));
}
cout<<"\nFile Updated Successfuly...\n";
}
fin.close();
fout.close();
}
void display_employee() //displays a particular the record
{
employee e1;
int id,counter=0;
int size = sizeoffile();
system("cls");
ifstream fin(file);
if(sizeoffile()==0) //return if DataFile.dat is empty. here the sizeoffile() is returning expected value.
{
cout<<"\nNo Record Found...\n";
fin.close();
return;
}
else
{
cout<<"\nEnter the Employee id...\n>";
cin>>id;
for(int i = 0;i<size;i++)
{
fin.read((char *)&e1,sizeof(e1));
if(e1.getid()==id)
{
cout<<"\n\tID\tNAME\tSALARY\n";
cout<<"================================================\n";
e1.display();
counter=1;
}
}
}
fin.close();
if(counter==0)
cout<<"\nRecord Not Found...\n";
}
void display_all()
{
employee e1;
int size = sizeoffile();
ifstream fin(file);
if(sizeoffile()==0) //sizeoffile again returning expected value.
{
cout<<"\nNo Record Found...\n";
fin.close();
return;
}
cout<<"\n\tID\t\tNAME\t\tSALARY\n";
cout<<"================================================\n";
for(int i = 0;i<size;i++)
{
fin.read((char *)&e1,sizeof(e1));
e1.display();
}
}
int sizeoffile() //this function returns the size of DataFile.dat in terms of records or objects of employee type.
{
employee eg;
int size,bytes;
ifstream fin(file);
fin.seekg(0,ios::end);
bytes = fin.tellg(); //assigning the size of file in terms of Bytes.
fin.close();
size = bytes/sizeof(eg); //converting the filesize from bytes to no. of records(ie. no. of objects of employee type.)
return size;
}
|