110 likes | 199 Views
File Input/Ouput. Professor Jason Chen School of Business Administration Gonzaga University Spokane, WA 99258 chen@gonzaga.edu. Logical Device/File. Processes on Input Stream. ifstream. ins. in_file. #define (physical file). Sumscore_in.txt. Logical Device/File.
E N D
File Input/Ouput Professor Jason Chen School of Business Administration Gonzaga University Spokane, WA 99258 chen@gonzaga.edu
Logical Device/File Processes on Input Stream ifstream ins in_file #define (physical file) Sumscore_in.txt
Logical Device/File Processes on Output Stream ofstream outs out_file #define (physical file) Sumscore_out.txt
Write onto the ofstream • You should have a carriage return on the last ofstream statement. E.g., • outs << "Average of the exam score is: " << average << endl;
// FILE: SumScore.cpp // ACCUMULATES THE SUM OF EXAM SCORES #include <iostream.> #include <conio.h> using namespace std; void main () { // Local data ... const int sentinel = -1; // sentinel value int count; int score; // each exam score int sum; // sum of scores float average; count =0; sum = 0; cout << "Enter scores one at a time as requested." << endl; cout << "When done, enter " << sentinel << " to stop." << endl; cout << "Enter the first score: "; cin >> score; while (score != sentinel) { count++; sum += score; cout << "Enter the next score : "; cin >> score; } // end while cout << endl << endl; cout << "Sum of exam scores is " << sum << endl; cout.setf(ios::showpoint); cout.setf(ios::fixed); cout.precision(2); average = float(sum)/float(count); cout << "Average of the exam score is: " << average << endl; }
// FILE: Sumscore_fio.cpp -- for External FIO version • // ACCUMULATES THE SUM OF EXAM SCORES • #include <iostream.> • #include <cstdlib> // for the def. of EXIT_FAILURE and ... • #include <fstream> //required for external file stream • using namespace std; • //Assoicate progrram identifiers with external file names • #define in_file "sumscore_in.txt" • #define out_file "sumscore_out.txt" • //return type of the "main" should be int (not void) • int main () • { • // Local data ... • const int sentinel = -1; // sentinel value • int count; • int score; // each exam score • int sum; // sum of scores • float average; • ifstreamins; //associates ins as an input stream • ofstreamouts; //associates outs as an outupt stream • //Open input and output files, exit on any error • ins.open (in_file); if (ins.fail()) { cerr << "*** ERROR; Cannot open " << in_file << " for input. \n"; getch(); return EXIT_FAILURE; // failure return } // end if outs.open (out_file); if (outs.fail()) { cerr << "*** ERROR; Cannot open " << out_file << " for output. \n"; return EXIT_FAILURE; // failure return } // end if
count =0; sum = 0; cout << "Scores will be entered from an external input file." << endl; cout << "When done, " << sentinel << " is entered to stop." << endl; //cout << "Enter the first score: "; ins >> score; while (score != sentinel) { count++; cout << "The " << count << " score is: " << score << endl; outs << "The " << count << " score is: " << score << endl; sum += score; //cout << "Enter the next score : "; ins >> score; } // end while cout << endl << endl; cout << "Sum of exam scores is " << sum << endl; cout.setf(ios::showpoint); cout.setf(ios::fixed); cout.precision(2); //for external output outs << endl << endl; outs << "Sum of exam scores is " << sum << endl; outs.setf(ios::showpoint); outs.setf(ios::fixed); outs.precision(2); average = float(sum)/float(count); cout << "Average of the exam score is: " << average << endl; outs << "Average of the exam score is: " << average << endl; //close input and output file streams ins.close(); outs.close(); return EXIT_SUCCESS; //successful return }//end of main //External input: sumscore_in.txt 9 4 8 99 -1 //External output: sumscore_out.txt The 1 score is: 9 The 2 score is: 4 The 3 score is: 8 The 4 score is: 99 Sum of exam scores is 120 Average of the exam score is: 30.00
Another Example • Payroll.cpp • Payroll_fio.cpp
// Prepare files. eds.open(inFile); if (eds.fail ()) { cerr << "*** ERROR: Cannot open " << inFile << " for input." << endl; return EXIT_FAILURE; // failure return } pds.open(outFile); if (pds.fail()) { cerr << "***ERROR: Cannot open " << outFile << " for output." << endl; eds.close(); return EXIT_FAILURE; // failure return } // Process all employees and compute total payroll. totalPayroll = processEmp(eds, pds); // Display result. cout << "Total payroll is " << totalPayroll << endl; pds << "Total payroll is " << totalPayroll << endl; // Close files. eds.close(); pds.close(); return 0; } // File: Payroll_File.cpp // Creates a company employee payroll file // computes total company payroll amount #include <fstream> // required for file streams #include <cstdlib> // for definition of EXIT_FAILURE #include <string> #include "money.cpp" using namespace std; // Associate streams with external file names #define inFile "Emp_File.txt" // employee file #define outFile "Salary.txt" // payroll file // Functions used ... // PROCESS ALL EMPLOYEES AND COMPUTE TOTAL money processEmp(istream, ostream); int main() { ifstream eds; // input: employee data stream ofstream pds; // output: payroll data stream money totalPayroll; // output: total payroll
// Insert processEmp here. // Process all employees and compute total payroll amount // Pre: eds and pds are prepared for input/output. // Post: Employee names and salaries are written from eds to pds // and the sum of their salaries is returned. // Returns: Total company payroll money processEmp (istream eds, // IN: employee file stream ostream pds) // IN: payroll file stream { string firstName; // input: employee first name string lastName; // input: employee last name float hours; // input: hoursWorked money rate; // input: hourly rate money salary; // output: gross salary money payroll; // return value - total company payroll payroll = 0.0; // Read first employee's data record. eds >> firstName >> lastName >> hours >> rate; while ( !eds.eof() ) { salary = hours * rate; pds << firstName << " " << lastName << " " << salary << endl; payroll += salary; // Read next employee's data record. eds >> firstName >> lastName >> hours >> rate; } // end while return payroll; } // end processEmp //Input file: Emp_File.txt Jim Baxter 35.5 7.25 Adrian Cybriwsky 40.0 6.50 Ayisha Mertens 20.0 8.00
HW# 7: House • Rewrite your house program using File I/O approach: • Read input from an external data file: house_in.txt • Write output to an external output file: house_out.txt • Save your file as: houseFIO_Lastname_Firstname.cpp Content of the house_in.txt: (you may create it using notepad) 67000 2300 2.5 62000 2500 2.5 75000 1850 2 Physical output file name should be: house_out.txt