// lpatrlst.cpp // ---------------------------------------------------------------------- // incomplete; see comments marked that w/ *** // definition for class LibraryPatronList // modified copy of ListA.h/cpp, Carrano et al. // // CSE 143 // Homework 3 // http://www.cs.washington.edu/education/courses/143/00su/homework/ // 02 Jul 2000; Ken Yasuhara #include #include #include #include #include "library.h" #include "lpatrlst.h" const int MAX_LINE_LENGTH = 120; // when reading from a file ////////////////////////////////////////////////////////////////////// // constructors LibraryPatronList::LibraryPatronList() { // *** your code here } LibraryPatronList::LibraryPatronList(char dataFileName[]) { // *** your code here // *** You should call private helper method readFile here. } ////////////////////////////////////////////////////////////////////// // methods // *** your code here ////////////////////////////////////////////////////////////////////// // I/O methods void LibraryPatronList::readFile(char fileName[]) { // Mac and UNIX users: omit ios::nocreate argument assert(strlen(fileName) > 0); ifstream inFile(fileName, ios::nocreate); if (!inFile) { cerr << endl << "unable to open file: " << fileName << endl; return; } int patronCount = 0; // counts number of patron records read so far while (inFile && (patronCount < MAX_PATRON_COUNT)) { int ID; inFile >> ID; // need to skip the trailing whitespace after ID, // i.e. trailing characters, including endline (\n) char line[MAX_LINE_LENGTH]; inFile.getline(line, MAX_LINE_LENGTH); char name[MAX_NAME_LENGTH]; inFile.getline(name, MAX_TITLE_LENGTH); if (strlen(name) == 0) { // stop at first blank line break; } // done reading data for this patron; insert in list LibraryPatron newPatron(ID, name); bool success; insertItem(newPatron, success); if (!success) { cerr << "LibraryPatronList::readFile(): ListInsert failed" << endl; return; } patronCount++; } inFile.close(); } // end readFile