Presentation is loading. Please wait.

Presentation is loading. Please wait.

9-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8.

Similar presentations


Presentation on theme: "9-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8."— Presentation transcript:

1

2 9-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8 Presentation Copyright 1999, Franklin, Beedle & Associates Students who purchase and instructors who adopt Computing Fundamentals with C++, Object-Oriented Programming and Design by Rick Mercer are welcome to use this presentation as long as this copyright notice remains intact.

3 9-2 Chapter 9 File Streams  Chapter Objectives  Use ifstream objects for disk file input  Use ofstream objects for disk file output  Apply the indeterminate loop pattern to process data until end of file

4 9-3 9.1 ifstream objects  ifstream objects  allow input from a disk file  are similar to istream objects cin  they share the same named operations  they use the same operator for input <<  must be initialized by the programmer  can be tested to determine if a disk file actually exists

5 9-4 ifstream objects continued  General form to construct ifstream objects ifstream object-name ( " file-name " ) ;  Sample code: ifstream inFile("myfile.dat"); ifstream inFile("myfile.dat");  this associates the object name inFile with the file named "myfile.dat" in the working folder.

6 9-5 Reading 3 numbers #include // for the ifstream class #include // needed for cout using namespace std; int main() { int n1, n2, n3; int n1, n2, n3; ifstream inFile("input.dat"); ifstream inFile("input.dat"); // Extract 3 integers from the file "input.dat" // Extract 3 integers from the file "input.dat" inFile >> n1 >> n2 >> n3; inFile >> n1 >> n2 >> n3; cout << "n1: " << n1 << endl; cout << "n1: " << n1 << endl; cout << "n2: " << n2 << endl; cout << "n2: " << n2 << endl; cout << "n3: " << n3 << endl; cout << "n3: " << n3 << endl; return 0; return 0;} Demonstrate 3nums.cpp 3nums.cpp

7 9-6 9.1.1 Getting the Path Right  It is easy to initialize an ifstream object and not have it associated with an actual disk file  perhaps the file does not exist  perhaps the path is wrong  perhaps you used \ to separate folder names  Recall escape sequences look like like \n \t  \ is also used to separate paths DOS and Windows ( Unix uses / so this is not an issue in the Unix environment): ifstream inFile("a:\myc++\input.dat"); // NO! ifstream inFile("a:\myc++\input.dat"); // NO! ifstream inFile("a:\\myc++\\input.dat"); // YES ifstream inFile("a:\\myc++\\input.dat"); // YES

8 9-7 Reading File names  However, when the user enters the file name, they may use one \, or in Unix, one / string filename; string filename; cout << "Enter file name: "; cout << "Enter file name: "; cin >> filename; cin >> filename; ifstream inFile( filename.c_str() ); ifstream inFile( filename.c_str() );  Dialogue in DOS/Windows Enter file name: c:\temp\in.dat Enter file name: c:\temp\in.dat  Dialogue in Unix Enter file name: /temp/in.dat Enter file name: /temp/in.dat  The ifstream class needs a C string literal like "in.dat", or the c_str() of the string as in filename.c.str(); // The C string literal filename.c.str(); // The C string literal

9 9-8 9.2 The Indeterminate Loop Pattern Applied to Disk Files  The end of file event can be used to terminate user input cout << "Enter numbers or end of file to quit\n"; cout << "Enter numbers or end of file to quit\n"; cout << "ctrl-D (UNIX)" << endl; cout << "ctrl-D (UNIX)" << endl; cout << "ctrl-Z (DOS or Windows) " << endl; cout << "ctrl-Z (DOS or Windows) " << endl; cout << "command-period (MacOs) " << endl; cout << "command-period (MacOs) " << endl; double x; double x; int n = 0; int n = 0; while(cin >> x) while(cin >> x) { // process x // process x n++; n++; } Demonstrate eof.cpp } Demonstrate eof.cpp

10 9-9 eof.cpp // pre: input.dat is in the current directory ifstream inFile("input.dat"); string s; if(!inFile) cout << "Error: 'input.dat' not found" << endl; cout << "Error: 'input.dat' not found" << endl; else { cout << "Before, eof = " << inFile.eof() << endl; cout << "Before, eof = " << inFile.eof() << endl; while(inFile >> s) { while(inFile >> s) { cout << "s = " << s << endl; cout << "s = " << s << endl; } cout << " After, eof = " << inFile.eof() << endl; cout << " After, eof = " << inFile.eof() << endl;}

11 9-10 9.2.1 Processing until End of File  We often need to extract data from files stored on a computer disk  We use ifstream objects for this kind of input  The next slide shows a program that averages numbers from the file a:\input.dat that has 70.080.090.075.085.0

12 9-11 Read until end of file #include // for class ifstream #include #include using namespace std; int main() { ifstream inFile("input.dat"); ifstream inFile("input.dat"); double number, sum = 0.0; double number, sum = 0.0; int n = 0; int n = 0; while(inFile >> number) { while(inFile >> number) { sum += number; sum += number; n++; n++; } cout << "Average: " << (sum / n) << endl; cout << "Average: " << (sum / n) << endl; return 0; return 0;}Output: Average: 70 Average: 70

13 9-12 9.3 Indeterminate Loop with More Complex Disk File Input  Assume this file data represents four employees each line represents one employee 40 8.88 1 S Demlow Mary 40 8.88 1 S Demlow Mary 42 7.77 2 M Barrister Harvey 42 7.77 2 M Barrister Harvey 0 10.00 3 M Manuala Ho 0 10.00 3 M Manuala Ho 38 9.99 0 S Kline Sue 38 9.99 0 S Kline Sue  Each line of data could be processed until end of file  Code should work with files of different sizes (different numbers of employees)

14 9-13 Algorithm to process any number of lines  For each employee in the file do the following  Input all data from one line in the file. This data represents one employee  Use data to construct one weeklyEmp object  Use two weeklyEmp member functions: grossPay() and name()

15 9-14 Example Code for eof loop cout.fill('*'); // set '*' as leading character while(inFile >> hours >> rate >> exemptions >> filingStatus >> lname >> fname ) >> filingStatus >> lname >> fname ){ emp = weeklyEmp(lname+", "+fname, hours, rate, emp = weeklyEmp(lname+", "+fname, hours, rate, exemptions, filingStatus); exemptions, filingStatus); cout.width(9); cout.width(9); cout << emp.grossPay()<<" "<< emp.name() << endl; cout << emp.grossPay()<<" "<< emp.name() << endl;}Output: ***355.20 DEMLOW, MARY ***355.20 DEMLOW, MARY ***334.11 BARRISTER, HARVEY ***334.11 BARRISTER, HARVEY *****0.00 MANUALA, HO *****0.00 MANUALA, HO ***379.62 KLINE, SUE ***379.62 KLINE, SUE

16 9-15 9.3.1 Mixing Numbers and Strings  Use care when input has a mix of numeric and alphanumeric data 40 8.88 1 S Demlow Mary 40 8.88 1 S Demlow Mary 42 7.77 M 2 Barrister Harvey 42 7.77 M 2 Barrister Harvey  Match input order to file data or vice versa  It is all too easy to get things out of order Unexpected character destroys the input stream

17 9-16 9.3.2 The getline Function  Use getline to read in lines of data as one string. Here is the function heading: istream & getline(istream & is, string & s, istream & getline(istream & is, string & s, char sentinel = '\n') char sentinel = '\n')  Examples: string name, address; getline(cin, address); // 1313 MockingB Lane getline(inFile, name, '.'); // Fatts, Ernest T. while( getline(infile, address) ) { // process address

18 9-17  It is also sometimes convenient to read an entire line of string data from the keyboard: Enter address: 1313 MockingBird Lane Enter address: 1313 MockingBird Lane  To do this, again use the C++ getline function void getline(istream& is, string& str) // post: Extracts string input from is (with blanks) // post: Extracts string input from is (with blanks) // until the end of line has been encountered // until the end of line has been encountered  Example function call string address; string address; cout << "Enter address: "; cout << "Enter address: "; getline(cin, address); getline(cin, address); getline used with cin

19 9-18 9.4 ofstream objects  The files storing large amounts of data are typically created by programs that send output to those files rather than the screen  The ofstream class is used to represent a disk file for output  General form: ofstream object-name ( "file-name" );

20 9-19 Sample Program #include // for class ifstream #include #include using namespace std; #include "compfun" // for decimals int main() { ofstream fout("c:\\temp\\output.dat"); ofstream fout("c:\\temp\\output.dat"); fout << "This does not go to the screen" << endl; fout << "This does not go to the screen" << endl; cout << "This does" << endl; cout << "This does" << endl; double x = 1.23456789; double x = 1.23456789; fout << x << endl; fout << x << endl; decimals(fout, 3); decimals(fout, 3); fout << x << endl; fout << x << endl; decimals(fout, 6); decimals(fout, 6); fout << x << endl; fout << x << endl; return 0; return 0;}


Download ppt "9-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN 1-887902-36-8."

Similar presentations


Ads by Google