70 likes | 141 Views
A Class Approach to Files of Records. Object Oriented Code for a Relative File. Parameters?. Design Questions. Private Data t he I/O file Record Count Private Methods Conversion from RRN to File Address. Public Methods Constructor Find / Retrieve Change Record Add Record
E N D
A Class Approach to Files of Records Object Oriented Code for a Relative File
Parameters? 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
Class Declaration class FacultyOfficeClass { public: FacultyOfficeClass(char *filename); intFind_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, intphone,int office, char *dept); private: fstreammyfile; // input AND output file intNum_Recs; // number of records in the file structfaculty_struct { char fname[15],lname[15]; int phone, office; char dept[5]; }; };
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<char *> (&Num_Recs), sizeof(int)); }
Change Record intFacultyOfficeClass::Set (intRRN,char *LName, char *FName, intphone,intoffice,char *dept) { intbyte_location; structfaculty_structnew_rec; /**** check for valid RRN ****/ if (RRN < 0 || 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(structfaculty_struct)); myfile.seekp (byte_location, ios::beg); myfile.write(reinterpret_cast<char *> (&new_rec), sizeof(structfaculty_struct)); /**** return success ****/ return 1; }
Retrieve intFacultyOfficeClass::Retrieve (intRRN,char *LName, char *FName, int &phone,int &office,char *dept) { intbyte_location; structfaculty_structnext_rec; /**** check for valid RRN ****/ if (RRN < 0 || RRN > Num_Recs-1) return -1; /**** go get the right record ****/ byte_location = sizeof(int) + (RRN * sizeof(structfaculty_struct)); myfile.seekg (byte_location, ios::beg); myfile.read(reinterpret_cast<char *> (&next_rec), sizeof(structfaculty_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; }
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;