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
|
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
struct Employee
{
int EmployeeID;
string EmployeeName;
string EmployeeDepartment;
string EmployeeDOB;
string EmployeeAddress;
int EmployeeWorkHours;
int PayRate;
int totalSalary;
};
fstream RecordFile;
const int RecordSize = 512;
const int UniqueIdPos = 0;
const int UniqueIdSize = 20;
const int RecordCountPos = UniqueIdSize + UniqueIdPos;
const int RecordCountSize = 20;
const int HeaderSize = UniqueIdSize + RecordCountSize;
int UniqueId = 0;
int RecordCount = 0;
bool readRecord(Employee *theEmployee , int index){
bool ok = false;
if(index < RecordCount) {
if(RecordFile.seekg(index * RecordSize + HeaderSize)){
RecordFile >> theEmployee->EmployeeID;
RecordFile.ignore(1, '\n');
getline(RecordFile, theEmployee->EmployeeName);
getline(RecordFile, theEmployee->EmployeeDepartment);
getline(RecordFile, theEmployee->EmployeeDOB);
getline(RecordFile, theEmployee->EmployeeAddress);
RecordFile >> theEmployee->EmployeeWorkHours;
RecordFile >> theEmployee->PayRate;
RecordFile >> theEmployee->totalSalary;
ok = RecordFile.good();
}
}
return ok;
}
void writeRecord(Employee *theEmployee , int index){
if(RecordFile.seekp(index * RecordSize + HeaderSize)){
ostringstream oss;
oss << theEmployee->EmployeeID << endl << theEmployee->EmployeeName << endl
<< theEmployee->EmployeeDepartment << endl
<< theEmployee->EmployeeDOB << endl << theEmployee->EmployeeAddress
<< endl << theEmployee->EmployeeWorkHours << endl << theEmployee->PayRate
<< endl << theEmployee->totalSalary << endl;
RecordFile.width(RecordSize);
RecordFile << left << oss.str();
if(index >= RecordCount)
{
RecordCount = index + 1;
if(RecordFile.seekp(RecordCountPos)){
RecordFile.width(RecordCountSize);
RecordFile << left << RecordCount;
}
}
}
}
void addRecord(Employee *theEmployee){
theEmployee->EmployeeID = UniqueId;
++UniqueId;
if(RecordFile.seekp(UniqueIdPos)){
RecordFile.width(UniqueIdSize);
RecordFile << left << UniqueId;
}
cout << "Enter Name : ";
getline( cin,theEmployee->EmployeeName );
cout << "Enter Employee Department : ";
getline( cin,theEmployee->EmployeeDepartment );
writeRecord(theEmployee, RecordCount);
}
void deleteSpecificEmployeeRecord(Employee *theEmployee){ // if done theEmployee contains the deleted record
int choice = 0;
int SpecificEmployeeID = 0;
bool found = 0;
int foundIndex = 0;
cout << "\tDelete Employee Record\n---------------------------------" << endl << endl
<< "Enter Employee ID : " ;
cin >> SpecificEmployeeID;
for( int i = 0 ; i < RecordCount ; i++ ){
if(readRecord(theEmployee, i)) {
if( theEmployee->EmployeeID == SpecificEmployeeID ){
found = true;
foundIndex = i;
break;
}
}
}
if( found == true ){
cout << "\n\tRecord :\n---------------------" << endl
<< "Employee ID : " << theEmployee->EmployeeID << endl
<< "Employee Name : " << theEmployee->EmployeeName << endl
<< "Employee Department : " << theEmployee->EmployeeDepartment << endl
<< "Employee Date Of Birth : " << theEmployee->EmployeeDOB << endl
<< "Employee Address : " << theEmployee->EmployeeAddress << endl
<< "Employee Work Hours : " << theEmployee->EmployeeWorkHours << endl
<< "Employee Pay Rate : " << theEmployee->PayRate << endl
<< "Employee Salary : " << theEmployee->totalSalary << endl << endl;
}
cout << "1. Delete Record " << endl
<< "2. Cancel " << endl;
cin >> choice;
if( choice == 1){
Employee e;
if(readRecord(&e, RecordCount - 1)) {
writeRecord(&e, foundIndex);
if(RecordFile.seekp((RecordCount - 1) * RecordSize + HeaderSize)){
RecordFile.width(RecordSize);
RecordFile << left << " ";
}
--RecordCount;
if(RecordFile.seekp(RecordCountPos)){
RecordFile.width(RecordCountSize);
RecordFile << left << RecordCount;
}
}
}
else if( choice == 2 ){
}
}
int main()
{
string filename;
cout << "Enter name of the filename: ";
getline( cin, filename );
RecordFile.open( filename.c_str() , fstream::in | fstream::out | fstream::binary );
if(RecordFile.is_open()) // if a file exists
{
if(RecordFile.seekg(UniqueIdPos))
RecordFile >> UniqueId;
if(RecordFile.seekg(RecordCountPos))
RecordFile >> RecordCount;
}
else // This means the file doesn't exist and we have to do some preparations
{
RecordFile.open( filename.c_str() , fstream::out | fstream::trunc | fstream::binary ); // Note that we cannot use fstream::in since it would fail
RecordFile.width(HeaderSize);
RecordFile << left << " ";
if(RecordFile.seekp(UniqueIdPos))
RecordFile << UniqueId;
if(RecordFile.seekp(RecordCountPos))
RecordFile << RecordCount;
RecordFile.close(); // Now the preparation is done and we can open the file regularly
RecordFile.open( filename.c_str() , fstream::in | fstream::out | fstream::binary );
}
if(RecordFile.is_open())
{
// Note: The following is for test purposes only. comment them in/out according to your needs
// Employee e0;
// deleteSpecificEmployeeRecord(&e0);
// Employee e;
// readRecord(&e, 1);
// Employee e1;
// addRecord(&e1);
// Employee e2;
// addRecord(&e2);
// Employee e3;
// addRecord(&e3);
RecordFile.close();
}
else
cout << "Cannot open File " << filename << endl;
return 0;
}
|