/* * * CSI2131 * * Jeff Souza * * Lab7 * */ #include #include #include #include // for setw, setiosflags, etc... using namespace std; // the number of records to read in const int num_records = 10; // the number of fields in each record const int num_fields = 5; // the length of the record length indicator const int length_ind_size = 2; // output length for each field const int output_lengths[] = {12,12,5,3,4}; // read a record by its RRN string readRecordbyRRN(int, istream&); // write a record using the report format void writeRecord(string, ostream&); // main method int main( ) { fstream input; // input stream fstream output; // output stream string rec; char inputname[40]; // input filename char outputname[40]; // output filename cout << "What is the input file name? "; cin >> inputname; cout << "What is the output (report) file name? "; cin >> outputname; // open the input file for input, of course input.open(inputname, ios::in); // Check to make sure that we could open the files properly! if (input.fail()) { cerr << "Could not open file "; cerr << inputname; cerr << ".\n"; return 1; } // open the output for output output.open(outputname, ios::out); // outout the header of the report output << "LAST NAME FIRST NAME MONTH DAY YEAR" << endl; output << "------------ ------------ ----- --- ----" << endl; // reads records in reverse order and copy the in output for (int i=num_records-1;i>=0; i--){ rec = readRecordbyRRN(i, input); writeRecord(rec, output); } cout << endl << "Program completed successfully !!" << endl; cout << "Check output file out." << endl << endl; input.close(); output.close(); return 0; } // read a student from a istream by its RRN string readRecordbyRRN(int rrn, istream& input) { char ind[length_ind_size]; // stores the length indicator digits int rec_size; // stores the length indicator string record = ""; // the record to be returned char aux; // auxiliar char input.seekg(0, ios::beg); // move read pointer to the beginning // read the length indicator and store it in rec_size input >> ind[0]; input >> ind[1]; rec_size = atoi(ind); // keep reading lenght indicator and skipping records for (int i=1;i<=rrn;i++){ input.seekg(rec_size-2, ios::cur); input >> ind[0]; input >> ind[1]; rec_size = atoi(ind); } // read the record chars for (int j=0;j> aux; record = record + aux; } // return the record return record; } // write a student in the ostream void writeRecord(string r, ostream& output) { string c, aux; // auxiliar strings int j=-1; // read all fields in the record for (int i=0;i