/* Datafile.cpp * * This is the cpp file for the datafile. * */ #include "Datafile.h" // the header for the student class #include // for setw, setiosflags, etc... // First constructor // This sets up the elements of student. Datafile::Datafile() { // Initialize name to be empty and number to be -1 number = -1; lastname = ""; firstname = ""; credits = -1; credits_term = -1; CGPA = 0; number_length = 8; lastname_length = 10; firstname_length = 10; credits_length = 3; CGPA_length = 4; recsize = number_length + lastname_length + firstname_length + credits_length + CGPA_length + 2; } // The destructor for the Student class. // We didn't allocate any memory using the "new" keyword, so we // don't have anything to do here. Datafile::~Datafile() { } // Accessors for the student number long int Datafile::getNumber() const { return number; } void Datafile::setNumber(long int newnumber) { number = newnumber; } // Accessors for the student lastname string Datafile::getLastName() const { return lastname; } void Datafile::setLastName(string newlastname) { lastname = newlastname; } // Accessors for the student firstname string Datafile::getFirstName() const { return firstname; } void Datafile::setFirstName(string newfirstname) { firstname = newfirstname; } // Accessors for the student credits int Datafile::getCredits() const { return credits; } void Datafile::setCredits(int newcredits) { credits = newcredits; } // Accessors for the student credits int Datafile::getCredits_term() const { return credits_term; } void Datafile::setCredits_term(int newcredits_term) { credits_term = newcredits_term; } // Accessors for the student CGPA float Datafile::getCGPA() const { return CGPA; } void Datafile::setCGPA(float newCGPA) { CGPA = newCGPA; } // seek to the appropriate record for reading void Datafile::InputPositioningByRRN(int RRN, istream& input) { input.seekg(RRN * recsize, ios::beg); } // seek to the appropriate position for writing void Datafile::OutputPositioningByRRN(int RRN, ostream& output) { output.seekp(RRN * recsize, ios::beg); } // modify the name of a student stored in a file void Datafile::ModifyBucketFile(int RRN, ostream& output, string newname) { // seek to the appropriate student OutputPositioningByRRN(RRN,output); // seek to the name field output.seekp(number_length, ios::cur); // write the new name over the old one output << setw(lastname_length+firstname_length) << setiosflags(ios::left) << newname << endl; }