Download presentation
Presentation is loading. Please wait.
1
Object Oriented Code for a Relative File
2
Design Questions Private Data the I/O file Record Count Private Methods Conversion from RRN to File Address Public Methods Constructor Find / Retrieve Change Record Add Record Delete Record Clean Up the File Parameters?
3
Class Declaration class FacultyOfficeClass { public: FacultyOfficeClass(char *filename); int Find_by_LName (char *LName); int Retrieve (int RRN, char *LName, char *FName, int &phone,int &office, char *dept); int Set (int RRN, char *LName, char *FName, int phone,int office, char *dept); private: fstream myfile; // input AND output file int Num_Recs; // number of records in the file struct faculty_struct { char fname[15],lname[15]; int phone, office; char dept[5]; };
4
Constructor FacultyOfficeClass::FacultyOfficeClass (char *filename) { /**** open the file ****/ myfile.open (filename, ios::in | ios::out); if (!myfile) { cerr << "Error opening input file:" << filename << endl; exit (1); } /**** read the number of records *****/ myfile.seekg (0, ios::beg); myfile.read(reinterpret_cast (&Num_Recs), sizeof(int)); }
5
Change Record int FacultyOfficeClass::Set (int RRN,char *LName, char *FName, int phone,int office,char *dept) { int byte_location; struct faculty_struct new_rec; /**** check for valid RRN ****/ if (RRN Num_Recs-1) return -1; /**** set all the fields ****/ strcpy (new_rec.lname, LName); strcpy (new_rec.fname, FName); new_rec.phone = phone; new_rec.office = office; strcpy (new_rec.dept, dept); /**** overwrite the correct record ****/ byte_location = sizeof(int) + (RRN * sizeof(struct faculty_struct)); myfile.seekp (byte_location, ios::beg); myfile.write(reinterpret_cast (&new_rec), sizeof(struct faculty_struct)); /**** return success ****/ return 1; }
6
Retrieve int FacultyOfficeClass::Retrieve (int RRN,char *LName, char *FName, int &phone,int &office,char *dept) { int byte_location; struct faculty_struct next_rec; /**** check for valid RRN ****/ if (RRN Num_Recs-1) return -1; /**** go get the right record ****/ byte_location = sizeof(int) + (RRN * sizeof(struct faculty_struct)); myfile.seekg (byte_location, ios::beg); myfile.read(reinterpret_cast (&next_rec), sizeof(struct faculty_struct)); /**** set all the parameters ****/ strcpy (LName, next_rec.lname); strcpy (FName, next_rec.fname); phone = next_rec.phone; office = next_rec.office; strcpy (dept, next_rec.dept); /**** return success ****/ return 1; }
7
main() - change a name case 2 : cout << "Current Last Name: "; cin >> lastname; RecNum = offices.Find_by_LName (lastname); if (RecNum == -1) { cout << "========================\n"; cout << " Not Found\n"; cout << "========================\n"; break; } else { offices.Retrieve (RecNum, lastname,firstname,… cout << "New Last Name: "; cin >> lastname; cout << "New First Name: "; cin >> firstname; offices.Set (RecNum, lastname,firstname,… } break;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.