Notes5 - Chapter 5 Chapter 5 I/O Streams I/O Streams Goals Goals • To determine how to use external files for input and output To determine how

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Chapter 5 Chapter 5 I/O Streams I/O Streams Goals: Goals: • To determine how to use external files for input and output To determine how to use external files for input and output • To investigate means by which file output can be formatted To investigate means by which file output can be formatted • To examine how to use I/O streams as function arguments To examine how to use I/O streams as function arguments • To introduce the concept of end-of-file for external input To introduce the concept of end-of-file for external input • To explore the nuances of character I/O with To explore the nuances of character I/O with get get & & put put Chapter 5 Chapter 5 CS 140 CS 140 Page Page 2 #include <iostream> #include <fstream> using namespace std; bool userWantsGrosses(); void outputTotalGross( double gross); void main() { int dayCount = 0; double dailyGross; double totalGross = 0.0; ifstream fileContainingGrosses; if (userWantsGrosses()) { fileContainingGrosses.open("GrossFile.txt"); while (dayCount < 365) { dayCount++; fileContainingGrosses >> dailyGross; totalGross += dailyGross; } fileContainingGrosses.close(); outputTotalGross(totalGross); } else cout << "Well, why did you bother " << "executing this program, " << "you goof?" << endl << endl; return ; } Input Files When a program When a program needs to retrieve a needs to retrieve a stream of data stream of data from an external from an external source other than source other than the keyboard, it the keyboard, it may use an input may use an input file stream. file stream. This library enables the This library enables the use of external files. use of external files. The declaration of The declaration of an an ifstream ifstream variable. variable. Opening the input Opening the input file in the current file in the current directory, using the directory, using the filename filename GrossFile.txt GrossFile.txt . . Reading the Reading the next value from next value from the input file. the input file. Closing the input Closing the input file. Note that the file. Note that the file name is not file name is not specified for closing. specified for closing. Chapter 5 Chapter 5 CS 140 CS 140 Page Page 3 bool userWantsGrosses() { char yOrN; cout << "Would you like to compute the " << “year\'s grosses? (Enter Y or N) "; cin >> yOrN; while ((yOrN != 'y') && (yOrN != 'Y') && (yOrN != 'n') && (yOrN != 'N')) { cout << "Your response must be the " << "letter \'Y\' or the letter \'N\'." << " Please try again: "; cin >> yOrN; } return ((yOrN == 'y') || (yOrN == 'Y')); } void outputTotalGross( double gross) { cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "The total gross is $"...
View Full Document

This note was uploaded on 08/26/2009 for the course CS 140 taught by Professor Staff during the Fall '08 term at Southern Illinois University Edwardsville.

Page1 / 12

Notes5 - Chapter 5 Chapter 5 I/O Streams I/O Streams Goals Goals • To determine how to use external files for input and output To determine how

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online