Chapter 10 InputOutput Streams Bjarne Stroustrup www stroustrup
Chapter 10 Input/Output Streams Bjarne Stroustrup www. stroustrup. com/Programming
2
Overview n n Fundamental I/O concepts Files Opening n Reading and writing streams n n n I/O errors Reading a single integer Stroustrup/Programming/2015 4
Input and Output data source: input device driver input library our program data destination: output library device driver Stroustrup/Programming/2015 output device 5
The stream model: views a continuous stream of flow of data c “somewhere” (1, 234) ostream 123 n buffer An ostream n n turns values of various types into character sequences sends those characters somewhere n E. g. , console, file, main memory, another computer Stroustrup/Programming/2015 6
The stream model: views a continuous stream of flow of data c “somewhere” (1, 234) istream 123 n buffer An istream n n turns character sequences into values of various types gets those characters from somewhere n E. g. , console, file, main memory, another computer Stroustrup/Programming/2015 7
The stream model n Reading and writing n Of typed entities n n Typically stored (entered, printed, etc. ) as text n n But not necessarily (see binary streams in chapter 11) Extensible n n << (output) and >> (input) plus other operations Type safe Formatted You can define your own I/O operations for your own types A stream can be attached to any I/O or storage device Stroustrup/Programming/2015 8
Files n We turn our computers on and off n n We like to keep our data n n So we keep what we want to preserve on disks and similar permanent storage A file is a sequence of bytes stored in permanent storage n n n The contents of our main memory is transient A file has a name The data on a file has a format We can read/write a file if we know its name and format Stroustrup/Programming/2015 9
A file 0: n n 1: 2: At the fundamental level, a file is a sequence of bytes numbered from 0 upwards Other notions can be supplied by programs that interpret a “file format” n For example, the 6 bytes "123. 45" might be interpreted as the floating-point number 123. 45 Stroustrup/Programming/2015 10
Files n General model disk Files (sequences of bytes) I/O system iostreams Stroustrup/Programming/2015 Main memory Objects (of various types) 11
Files n To read a file n n We must know its name (and assign a variable with the name) We must open it (for reading) Then we can read Then we must close it n n That is typically done implicitly (but a good practice is to close it) To write a file n n We must name it We must open it (for writing) n n n Or create a new file of that name Then we can write it We must close it n That is typically done implicitly Stroustrup/Programming/2015 12
Opening a file for reading // … int main() { cout << "Please enter input file name: "; // goes to default o/p device: terminal string iname; // this a variable, not the file-name cin >> iname; // read file name from default i/p device: terminal ifstream ist {iname}; // ifstream is an“input stream from a file” // defining an ifstream with a name string // opens the file of that name for reading // now we attach file name: Initialize ist that is std: : ifstream class if (!ist) error("can’t open input file ", iname); // … Stroustrup/Programming/2015 13
Opening a file for writing // … cout << "Please enter name of output file: "; string oname; cin >> oname; ofstream ofs {oname}; // ofstream is an “output stream from a file” // defining an ofstream with a name string // opens the file with that name for writing if (!ofs) error("can’t open output file ", oname); // … } Stroustrup/Programming/2015 14
#include "std_lib_facilities. h" struct Reading { int hour; double temperature; }; int main() { cout << "Please enter input file name: "; string iname; cin >> iname; ifstream ist {iname}; if (!ist) error("can't open input file ", iname); string oname; cout << "Please enter name of output file: "; cin >> oname; ofstream ost {oname}; if (!ost) error("can't open output file ", oname); }
………. . ………… vector<Reading> temps; // store the readings here int hour; double temperature; while (ist >> hour >> temperature) { if (hour < 0 || 23 <hour) error("hour out of range"); temps. push_back(Reading{hour, temperature}); } for (int i=0; i<temps. size(); ++i) ost << '(' << temps[i]. hour << ', ' << temps[i]. temperature << ")n"; ……………
Reading from a file n Suppose a file contains a sequence of pairs representing hours and temperature readings 0 60. 7 1 60. 6 2 60. 3 3 59. 22 n n The hours are numbered 0. . 23 No further format is assumed n n Maybe we can do better than that (but not just now) Termination n n Reaching the end-of-file terminates the read Anything unexpected in the file terminates the read n E. g. , q Stroustrup/Programming/2015 17
Reading a file struct Reading { // a temperature reading int hour; // hour after midnight [0: 23] double temperature; }; vector<Reading> temps; // create a vector to store the readings int hour; double temperature; while (ist >> hour >> temperature) { if (hour < 0 || 23 <hour) error("hour out of range"); temps. push_back( Reading{hour, temperature} ); } Stroustrup/Programming/2015 // read // check // store 18
I/O error handling n Sources of errors n n n Human mistakes Files that fail to meet specifications Specifications that fail to match reality Programmer errors Etc. iostream reduces all errors to one of four states n n good() eof() fail() bad() // the operation succeeded // we hit the end of input (“end of file”) // something unexpected happened // something unexpected and serious happened Stroustrup/Programming/2015 19
Sample integer read “failure” n Ended by “terminator character” n n n Ended by format error n n n 1 2 3 4 5. 6 State is fail() Ended by “end of file” n n n 1 2 3 4 5 * State is fail() 1 2 3 4 5 end of file 1 2 3 4 5 Control-Z (Windows) 1 2 3 4 5 Control-D (Unix) State is eof() Something really bad n n Disk format error State is bad() Stroustrup/Programming/2015 20
int i = 0; cin >> i; if (!cin) { // we get here (only) if an input operation failed if (cin. bad()) error("cin is bad"); // stream corrupted: let’s get out of here! if (cin. eof()) { // no more input // this is often how we want a sequence of input operations to end } if (cin. fail()) { // stream encountered something unexpected cin. clear(); // make ready for more input // somehow recover } }
I/O error handling void fill_vector(istream& ist, vector<int>& v, char terminator) { // read integers from ist into v until we reach eof() or terminator for (int i; ist >> i; ) // read until “some failure” v. push_back(i); // store in v if (ist. eof()) return; // fine: we found the end of file if (ist. bad()) error("ist is bad"); // stream corrupted; let’s get out of here! if (ist. fail()) { // clean up the mess as best we can and report the problem ist. clear(); // clear stream state, so that we can look for terminator char c; ist >> c; // read a character, hopefully terminator if (c != terminator) { // unexpected character ist. unget(); // put that character back ist. clear(ios_base: : failbit); // set the state back to fail() } } } Stroustrup/Programming/2015 22
Throw an exception for bad() // How to make ist throw if it goes bad: ist. exceptions()|ios_base: : badbit); // can be read as // “set ist’s exception mask to whatever it was plus badbit” // or as “throw an exception if the stream goes bad” Given that, we can simplify our input loops by no longer checking for bad Stroustrup/Programming/2015 23
// open an input file: cout << "Please enter input file namen"; string iname; cin >> iname; ifstream ist {iname}; if (!ifs) error("can't open input file", iname); ifs. exceptions()|ios_base: : badbit); // throw for bad() // open an output file: cout << "Please enter output file namen"; string oname; cin >> oname; ofstream ost {oname}; if (!ofs) error("can't open output file", oname); // read an arbitrary number of years: vector<Year> ys; while(true) { Year y; // get a freshly initialized Year each time around if (!(ifs>>y)) break; // reading within if-check ys. push_back(y); } cout << "read " << ys. size() << " years of readingsn"; for (Year& y : ys) print_year(ofs, y);
Simplified input loop void fill_vector(istream& ist, vector<int>& v, char terminator) { // read integers from ist into v until we reach eof() or terminator for (int i; ist >> i; ) v. push_back(i); if (ist. eof()) return; // fine: we found the end of file // not good() and not bad() and not eof(), ist must be fail() ist. clear(); // clear stream state char c; ist >> c; // read a character, hopefully terminator if (c != terminator) { // ouch: not the terminator, so we must fail ist. unget(); // maybe my caller can use that character ist. clear(ios_base: : failbit); // set the state back to fail() } } Stroustrup/Programming/2015 25
Reading a single value // first simple and flawed attempt: cout << "Please enter an integer in the range 1 to 10 (inclusive): n"; int n = 0; while (cin>>n) { // read if (1<=n && n<=10) break; // check range, yeh, what if input ‘t’? cout << "Sorry, " << n << " is not in the [1: 10] range; please try againn"; } // use n here n Three kinds of problems are possible n n n the user types an out-of-range value getting no value (end of file) the user types something of the wrong type (here, not an integer) Stroustrup/Programming/2015 26
Reading a single value n What do we want to do in those three cases? n handle the problem in the code doing the read? throw an exception to let someone else handle the problem (potentially terminating the program)? ignore the problem? n Reading a single value n n Is something we often do many times We want a solution that’s very simple to use Stroustrup/Programming/2015 27
Handle everything: What a mess! cout << "Please enter an integer in the range 1 to 10 (inclusive): n"; int n = 0; while (cin >> n) { if (cin) { // we got an integer; now check it: if (1<=n && n<=10) break; cout << "Sorry, " << n << " is not in the [1: 10] range; please try againn"; } else if (cin. fail()) { // we found something that wasn’t an integer cin. clear(); // we’d like to look at the characters cout << "Sorry, that was not a number; please try againn"; for (char ch; cin>>ch && !isdigit(ch); ); // throw away non-digits /* do nothing */ ; if (!cin) error("no input"); // we didn’t find a digit: give up cin. unget(); // put the digit back, so that we can read the number } else error("no input"); // eof or bad: give up } // if we get here n is in [1: 10] Stroustrup/Programming/2015 28
The mess: trying to do everything at once n Problem: We have all mixed together n n n reading values prompting the user for input writing error messages skipping past “bad” input characters testing the input against a range Solution: Split it up into logically separate parts Stroustrup/Programming/2015 29
What do we want? n n What logical parts do we what? n int get_int(int low, int high); // read an int in [low. . high] from cin n int get_int(); // read an int from cin // so that we can check the range int n void skip_to_int(); // we found some “garbage” character // so skip until we find an int Separate functions that do the logically separate actions Stroustrup/Programming/2015 30
Skip “garbage” void skip_to_int() { if (cin. fail()) { // we found something that wasn’t an integer cin. clear(); // we’d like to look at the characters for(char ch; cin>>ch; ) { // throw away non-digits if (isdigit(ch) || ch=='-') { cin. unget(); // put the digit back, // so that we can read the number return; } } } error("no input"); // eof or bad: give up } Stroustrup/Programming/2015 31
Get (any) integer int get_int() { int n = 0; while (true) { if (cin >> n) return n; cout << "Sorry, that was not a number; please try againn"; skip_to_int(); } } Stroustrup/Programming/2015 32
Get integer in range int get_int(int low, int high) { cout << "Please enter an integer in the range " << low << " to " << high << " (inclusive): n"; while (true) { int n = get_int(); if (low<=n && n<=high) return n; cout << "Sorry, " << n << " is not in the [" << low << ': ' << high << "] range; please try againn"; } } Stroustrup/Programming/2015 33
Use int n = get_int(1, 10); cout << "n: " << n << endl; int m = get_int(2, 300); cout << "m: " << m << endl; n Problem: n The “dialog” is built into the read operations Stroustrup/Programming/2015 34
What do we really want? // parameterize by integer range and “dialog” int strength = get_int(1, 10, "enter strength", "Not in range, try again"); cout << "strength: " << strength << endl; int altitude = get_int(0, 50000, "please enter altitude in feet", "Not in range, please try again"); cout << "altitude: " << altitude << "ft. above sea leveln"; n n n That’s often the really important question Ask it repeatedly during software development As you learn more about a problem and its solution, your answers improve Stroustrup/Programming/2015 35
Parameterize int get_int(int low, int high, const string& greeting, const string& sorry) { cout << greeting << ": [" << low << ': ' << high << "]n"; while (true) { int n = get_int(); if (low<=n && n<=high) return n; cout << sorry << ": [" << low << ': ' << high << "]n"; } } n Incomplete parameterization: get_int() still “blabbers” n n “utility functions” should not produce their own error messages Serious library functions do not produce error messages at all n They throw exceptions (possibly containing an error message) Stroustrup/Programming/2015 36
User-defined output: operator<<() n Usually trivial ostream& operator<<(ostream& os, const Date& d) { return os << '(' << d. year() << ', ' << d. month() << ', ' << d. day() << ')'; } n We often use several different ways of outputting a value n Tastes for output layout and detail vary Stroustrup/Programming/2015 37
Use void do_some_printing(Date d 1, Date d 2) { cout << d 1; // means operator<<(cout, d 1) ; cout << d 1 << d 2; // means (cout << d 1) << d 2; // means (operator<<(cout, d 1)) << d 2; // means operator<<((operator<<(cout, d 1)), d 2) ; } Stroustrup/Programming/2015 38
User-defined input: operator>>() istream& operator>>(istream& is, Date& dd) // Read date in format: ( year , month , day ) { int y, d, m; char ch 1, ch 2, ch 3, ch 4; is >> ch 1 >> y >> ch 2 >> m >> ch 3 >> d >> ch 4; if (!is) return is; // we didn’t get our values, so just leave if (ch 1!='(' || ch 2!=', ' || ch 3!=', ' || ch 4!=')') { // oops: format error is. clear(ios_base: : failbit); // something wrong: set state to fail() return is; // and leave } dd = Date{y, Month(m), d}; // update dd return is; // and leave with is in the good() state } Stroustrup/Programming/2015 39
Strange input format: { year 1990 } {year 1991 { month jun }} { year 1992 { month jan ( 1 0 61. 5) } {month feb (1 1 64) (2 2 65. 2) } } {year 2000 { month feb (1 1 68 ) (2 3 66. 66 ) ( 1 0 67. 2)} {month dec (15 15 – 9. 2 ) (15 14 – 8. 8) (14 0 – 2) } }
bad() buffer clear() close() device driver eof() fail() file good() ifstream input device input operator iostream istream ofstream open() ostream ouput device ouput operator stream state structured file terminator unget() Review 1. When dealing with input and output, how is the variety of devices dealt with in most modern computers? 2. What, fundamentally, does an istream do? 3. What, fundamentally, does an ostream do? 4. What, fundamentally, is a file? 5. What is a file format? 6. Name four different types of devices that can require I/O for a program. 7. What are the four steps for reading a file? 8. What are the four steps for writing a file? 9. Name and define the four stream states. 10. Discuss how the following input problems can be resolved: a. The user typing an out-of-range value b. Getting no value (end of file) c. The user typing something of the wrong type 11. In what way is input usually harder than output? 12. In what way is output usually harder than input? 13. Why do we (often) want to separate input and output from computation? 14. What are the two most common uses of the istream member function clear()? 15. What are the usual function declarations for << and >> for a user-defined type X?
Next Lecture Customizing input and output (chapter 11) Stroustrup/Programming/2015 42
- Slides: 41