//as6.cpp //CS010 Winter 2004 //This program takes student data from a file called raw.dat and outputs it //in a more readable format along with header information to a file called //nice.dat. #include #include #include #include using namespace std; struct Date { int month; int day; int year; }; struct StudentRecord { int sid; double gpa; Date dob; int comp_units; int curr_units; Date grad; }; void intro(); //precondition: none //postcondition: An introduction to the program is output to the screen. void getRecord(ifstream& fin, StudentRecord& student); //preconditions: The parameter fin is connected to a file that has student // records in raw form (just the data separated by whitespace). // fin should point to the beginning of a student's record. // The parameter student has been declared. //postconditions: The parameter fin points to the next student's sid or end // of file. // The parameter student has all member variables initialized to // data from the file connected to fin. void outputRecord(ofstream& fout, StudentRecord& student); //preconditions: The parameter fout is connected to a file that has its // header information and possibly some number of other student's // records already printed. fout should point to the beginning // of the next blank line. //postconditions: The file connected to fout has the data from the parameter // student printed to it lined up with the header information. // fout now points to the beginning of the next line. void outputHeader(ofstream& fout); //precondition: The parameter fout is connected to the output file. //postcondition: The header information has been printed to the output file. int main() { // used to pass StudentRecord object from input file to output file. StudentRecord next_student; ifstream infile; ofstream outfile; int num_records; char cont; intro(); // Open input and output files and check for errors. // Exit the program if any file does not open. infile.open("raw.dat"); if (infile.fail()) { cout << "Error opening input file\n"; exit(1); } outfile.open("nice.dat"); if (outfile.fail()) { cout << "Error opening output file\n"; exit(1); } cout << "How many student records are in the input file, raw.dat?\n"; cin >> num_records; // output header before entering loop that outputs all student records. outputHeader(outfile); // each iteration gets one student record from the input file and outputs it // to the output file. for (int i = 0; i < num_records; ++i) { getRecord(infile, next_student); outputRecord(outfile, next_student); } cout << "\nThe file nice.dat now holds the student records.\n\n"; // close all open file objects infile.close(); outfile.close(); cout << "Press any key to continue\n"; cin >> cont; return 0; } // end main void intro() { cout << "\nThis program takes the raw student data from an input file named" << " raw.dat and outputs it in a more readable format to the file" << " nice.dat.\n\n"; } void outputHeader(ofstream& fout) { fout << " SID GPA DOB Total Units Current Units Grad Date\n" << "---- ---- ---------- ----------- ------------- ----------\n"; } void getRecord(ifstream& fin, StudentRecord& student) { fin >> student.sid >> student.gpa >> student.dob.month >> student.dob.day >> student.dob.year >> student.comp_units >> student.curr_units >> student.grad.month >> student.grad.day >> student.grad.year; } void outputRecord(ofstream& fout, StudentRecord& student) { // cause gpa to always output 2 digits after decimal point fout.setf(ios::fixed); fout.setf(ios::showpoint); fout.precision(2); // all data will output to right of given width by default fout << student.sid << setw(6) << student.gpa << setw(4) << student.dob.month << '/' << setw(2) << student.dob.day << '/' << setw(4) << student.dob.year << setw(9) << student.comp_units << setw(14) << student.curr_units << setw(11) << student.grad.month << '/' << setw(2) << student.grad.day << '/' << setw(4) << student.grad.year << endl; }