1 / 7

A Class Approach to Files of Records

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

kiana
Download Presentation

A Class Approach to Files of Records

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Class Approach to Files of Records Object Oriented Code for a Relative File

  2. 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

  3. 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]; }; };

  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<char *> (&Num_Recs), sizeof(int)); }

  5. 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; }

  6. 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; }

  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;

More Related