Files of Records Object Oriented Code for a Relative File Design Questions Private Data t he IO file Record Count Private Methods Conversion from RRN to File Address Public Methods Constructor ID: 184021
Download Presentation The PPT/PDF document "A Class Approach to" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
A Class Approach to Files of Records
Object Oriented Code for a Relative FileSlide2
Design Questions
Private Data
t
he I/O fileRecord CountPrivate MethodsConversion from RRN to File Address
Public MethodsConstructorFind / RetrieveChange RecordAdd RecordDelete RecordClean Up the File
Parameters?Slide3
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];
};
};Slide4
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
));
}Slide5
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 < 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
(
struct
faculty_struct
));
myfile.seekp
(
byte_location
,
ios
::beg);
myfile.write
(
reinterpret_cast
<char *> (&
new_rec
),
sizeof
(
struct
faculty_struct
));
/**** return success ****/
return 1;
}Slide6
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 < 0 || 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
<char *> (&
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;
}Slide7
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
;