// Program to main Employee database and allow all common operations. // Assignment-35 // empdbapp.cpp, Author - Krishna Kumar Khatri. // Uncomment ONLY the following line if you are using Turbo/Borland compiler. // #define TC #ifdef TC #include #include #include #include #else #include #include #include #include using namespace std; #endif #include // For getch. #include // For system. #include // For remove and rename. const int MAXLEN = 30; class Employee { int empid; char empname[MAXLEN]; char designation[MAXLEN / 2]; char qualification[MAXLEN / 2]; double salary; public: void getdata(); void putdata(); void repdata(); // Tabular report. int getempid(); // Accessor. char* getempname(); // Accessor. }; void Employee::getdata() { cout << "Enter Employee ID: "; cin >> empid; cout << "Name: "; cin >> empname; cout << "Designation: "; cin >> designation; cout << "Qualification: "; cin >> qualification; cout << "Salary: "; cin >> salary; } void Employee::putdata() { cout << "Emp ID - " << empid << endl; cout << "Name - " << empname << endl; cout << "Dsgnt. - " << designation << endl; cout << "Qualf. - " << qualification << endl; cout << "Salary - " << salary; } void Employee::repdata() { cout << setw(4) << empid << " " << setw(12) << setiosflags(ios::left) << empname << " " << setw(12) << designation << " " << setw(12) << qualification << " " << "$" << setw(12) << salary; } int Employee::getempid() { return empid; } char* Employee::getempname() { return empname; } long filesize(char* filename) { fstream fn; long b, e; int reccnt; fn.open(filename, ios::in); fn.seekg(0, ios::beg); b = fn.tellg(); fn.seekg(0, ios::end); e = fn.tellg(); reccnt = e / sizeof(Employee); cout << endl; reccnt == 0 ? cout << "Empty database." : cout << setw(3) << reccnt << " record(s)"; return (e - b); } int main() { #ifdef TC clrscr(); #endif char* filename = "Empdb.txt"; char* newfilnm = "Tmpdb.txt"; ofstream ofs; ifstream ifs; ofstream tmpf; char choice; Employee emp; int empid; int flag = 'f'; char empname[MAXLEN]; do { cout << "------------------------------" << endl; cout << " Employee Database Management " << endl; cout << "------------------------------" << endl; cout << "[a] Add new record." << endl; cout << "[u] Update existing record." << endl; cout << "[q] Query the database." << endl; cout << "[s] Display all records." << endl; cout << "[d] Delete the record." << endl; cout << "[x] Exit." << endl << endl; cout << "Your choice: ["; choice = getche(); cout << ']'; switch (choice) { case 'a': cout << endl << endl << "Record details for employee," << endl; emp.getdata(); ofs.open(filename, ios::app); // Append mode. ofs.write((char*)&emp, sizeof(emp)); ofs.close(); cout << "Record written."; break; case 'u': cout << endl << endl << "Updating record." << endl; cout << "Enter Emp ID (reference): "; cin >> empid; ifs.open(filename); tmpf.open(newfilnm); flag = 'f'; ifs.read((char*)&emp, sizeof(emp)); while (ifs) { if (emp.getempid() == empid) { cout << endl << "Entries for matching record are," << endl; emp.putdata(); cout << endl << endl << "Enter new data," << endl; emp.getdata(); flag = 't'; } tmpf.write((char*)&emp, sizeof(emp)); ifs.read((char*)&emp, sizeof(emp)); } ifs.close(); tmpf.close(); remove(filename); rename(newfilnm, filename); if (flag == 't') cout << endl << "Record updated."; else cout << endl << "Matching record not found!"; break; case 'q': cout << endl << endl << "Querying for records." << endl << endl; cout << "Query for," << endl; do { cout << "[i] Employee ID." << endl; cout << "[n] Employee name." << endl << endl; cout << "Your choice (i/n): ["; choice = getche(); cout << ']'; } while (choice != 'i' && choice != 'n'); ifs.open(filename); switch (choice) { case 'i': cout << endl << "Enter Emp ID (reference): "; cin >> empid; while (ifs) { ifs.read((char*)&emp, sizeof(emp)); if (emp.getempid() == empid) { cout << endl << "Record found." << endl; emp.putdata(); break; } } break; case 'n': cout << endl << "Enter Emp name (reference): "; cin >> empname; while (ifs) { ifs.read((char*)&emp, sizeof(emp)); if (strcmp(emp.getempname(), empname) == 0) { cout << endl << "Record found." << endl; emp.putdata(); break; } } break; default: cout << "Invalid choice."; break; } if (!ifs) cout << endl << "Matching record not found!"; ifs.close(); break; case 's': if (filesize(filename) == 0L) break; cout << " affected." << endl; cout << "------------------------" << endl; cout << " ID NAME DSGNT "; cout << " QUALF SALARY " << endl; cout << "---- ------------ --------"; cout << "---- ------------ -------------" << endl; ifs.open(filename); ifs.read((char*)&emp, sizeof(emp)); while (!ifs.eof()) { emp.repdata(); cout << endl; ifs.read((char*)&emp, sizeof(emp)); } ifs.close(); break; case 'd': cout << endl << endl << "Deleting record." << endl; cout << "Enter Emp ID (reference): "; cin >> empid; ifs.open(filename); tmpf.open(newfilnm); flag = 'f'; ifs.read((char*)&emp, sizeof(emp)); while (ifs) { if (emp.getempid() == empid) { cout << endl << "Entries for matching record are," << endl; emp.putdata(); cout << endl << endl << "Permanently delete this record (y/n): "; choice = getche(); if (choice != 'y') tmpf.write((char*)&emp, sizeof(emp)); flag = 't'; } else tmpf.write((char*)&emp, sizeof(emp)); ifs.read((char*)&emp, sizeof(emp)); } ifs.close(); tmpf.close(); remove(filename); rename(newfilnm, filename); if (flag == 't') { if (choice == 'y') cout << endl << "Record deleted."; else cout << endl << "Record not deleted."; } else cout << endl << "Matching record not found!"; break; case 'x': // Do nothing. break; default: cout << endl << endl << "Invalid choice."; break; } if (choice != 'x') { cout << endl << endl << "Press any key to continue..."; getch(); } cout << endl << endl; } while (choice != 'x'); cout << endl << "Program over. Press any key to exit..."; getch(); return 0; // Successful termination. }