360 likes | 523 Views
17-Jul-2006. Today’s Objectives. Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism, templates, and stream I/O
E N D
17-Jul-2006 Today’s Objectives • Announcements • Turn in Homework 4 • Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism, templates, and stream I/O • Homework 5 is due on next Monday, July 24. Since this date is so close to the end of the semester, no late assignments will be accepted and email will NOT be accepted! • Stream Input/Output (Ch. 15) • Streams • I/O in C++ with streams • Stream error states • Stream manipulators • Bonus Lab 8 – Debugging with MS Visual Studio
Stream Input/Output Chapter 15
Stream I/O (Deitel, 771; Josuttis, 584) Streams • Stream = a sequence of bytes • Just like a water-filled stream, a C++ stream has a flow, it can be directed, and it can be stopped. But it cannot flow backwards. • In C++, I/O is accomplished by using streams • A C++ stream is an object – it’s instantiated from a class, just like any other object • Output – the bytes flow from memory to an output device • Input – the bytes flow from an input device to memory
Stream I/O (Deitel, 771–772) I/O in C++ • Low-level I/O is unformatted I/O • Specifies the number of bytes to transfer • Example: member functions such as “get”, “put”, “read”, and “write” • High-level I/O is formatted I/O • Uses bytes in groups corresponding to built-in data types • Example:int n;cin >> n; • wchar_t = a C++ data type used to store Unicode characters
Stream I/O (Deitel, 772) Header Files <iostream> • Basic stream I/O services • Defines objects: cout, cin, cerr, and clog <iomanip> • Services for formatted I/O with stream manipulators <fstream> • Services for file I/O
Stream I/O (Deitel, 772–773; Josuttis, 585) Stream I/O Classes • Different classes are used to create stream objects for each type of I/O, Fig. 15.1, page 773 • istream • Defines input streams • Instantiation of the template class basic_istream<> using char as the type parameter • ostream • Defines output streams • Instantiation of the template class basic_ostream<> using char as the type parameter
Stream I/O (Deitel, 772–773; Josuttis, 585) Global Stream Objects cin • Predefined object of the istream class • Used for standard user input, normally connected to the keyboard cout • Predefined object of the ostream class • Used for standard user output, normally connected to the monitor cerr • Predefined object of the ostream class • Used for error messages, not buffered, normally connected to the monitor clog • Predefined object of the ostream class • Used for logging messages, buffered, normally connected to the same device as cerr
Stream I/O (Deitel, 775–776; Josuttis, 586) Stream Output • operator<< • Stream insertion operator • Overloaded for output to ostream objects • It’s typesafe because it’s overloaded for all the built-in data types, and it can be overloaded for our own classes • Member function put • Member function of the ostream class • Used to output single chars • Examples cout.put( 'A' ); cout.put( 65 ); //Using the ASCII value
Stream I/O (Deitel, 776–780; Josuttis, 586) Stream Input • operator>> • Stream extraction operator • Overloaded for input from istream objects • It’s typesafe because it’s overloaded for all the built-in data types, and it can be overloaded for our own classes • Ignores whitespace and leaves it in the stream • Member functions of the istream class get, getline ignore eof, gcount putback, peek
Stream I/O (Deitel, 776–780) Using get() • Used with no arguments, it inputs one char char c = cin.get(); • Can be used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n');
Stream I/O (Deitel, 776–780) Using get() • Used with no arguments, it inputs one char char c = cin.get(); • Can be used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); cin \n l l e o H buffer
H Stream I/O (Deitel, 776–780) Using get() • Used with no arguments, it inputs one char char c = cin.get(); • Can be used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); cin l o l \n e buffer
H e Stream I/O (Deitel, 776–780) Using get() • Used with no arguments, it inputs one char char c = cin.get(); • Can be used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); cin o \n l l buffer
H e l Stream I/O (Deitel, 776–780) Using get() • Used with no arguments, it inputs one char char c = cin.get(); • Can be used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); cin \n o l buffer
H e l l Stream I/O (Deitel, 776–780) Using get() • Used with no arguments, it inputs one char char c = cin.get(); • Can be used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); cin \n o buffer
H e l l o Leaves the delimiter in the stream Stream I/O (Deitel, 776–780) Using get() • Used with no arguments, it inputs one char char c = cin.get(); • Can be used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); cin \n buffer
H e l l o \0 Stream I/O (Deitel, 776–780) Using get() • Used with no arguments, it inputs one char char c = cin.get(); • Can be used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.get(buffer,1024,'\n'); cin \n Inserts ‘\0’ terminator buffer
Stream I/O (Deitel, 776–780) Using getline() • Used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024);
Stream I/O (Deitel, 776–780) Using getline() • Used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); cin \n l l e o H buffer
H Stream I/O (Deitel, 776–780) Using getline() • Used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); cin l o l \n e buffer
H e Stream I/O (Deitel, 776–780) Using getline() • Used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); cin o \n l l buffer
H e l Stream I/O (Deitel, 776–780) Using getline() • Used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); cin \n o l buffer
H e l l Stream I/O (Deitel, 776–780) Using getline() • Used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); cin \n o buffer
H e l l o Discards the delimiter in the stream Stream I/O (Deitel, 776–780) Using getline() • Used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); cin \n buffer
H e l l o \0 Stream I/O (Deitel, 776–780) Using getline() • Used with three arguments • char array • size limit • delimiter char (optional: default is ‘\n’) char buffer[1024]; cin.getline(buffer,1024); cin Inserts ‘\0’ terminator buffer
Stream I/O (Deitel, 776–780) Using read() and write() • Input • Member function read of the istream class • Inputs bytes into a char array • Output • Member function write of the ostream class • Outputs bytes in a char array • Example char buffer[1024]; cin.read( buffer, 5 ); cout.write( buffer, 5 );
Stream I/O (Deitel, 766–768; Josuttis 597) Stream Error States • A data member of the stream objects identifies whether I/O was successful with the following values • goodbit = everything okay, normally set to 0 • eofbit = end-of-file encountered • failbit = operation not successful, but the stream is okay, e.g. char in the stream instead of an integer • badbit = operation not successful, because stream is corrupted or lost, e.g. reading past the end of a file
Stream I/O (Deitel, 797–799; Josuttis 597) Determining the Stream State • Use the following member functions to determine the state of the flags • good() = returns true if the stream is okay • eof() = returns true if the end-of-file was encountered • fail() = returns true if there was an I/O error • operator! = same as fail() • bad() = returns true if there was a fatal error • clear() = clears all flags
Stream I/O Example: Checking Stream State int main(){ int n = 1; char c = 'x'; while( n > 0 ){ cout << "Enter an integer: "; cin >> n; if( !cin ){ //check the failbit cout << "That was not an integer!\n"; cin.clear(); //clear the flags cin >> c; //something is still in the stream cout << "You entered " << c << '\n'; } else cout << "You entered " << n << '\n'; } }
Manipulators that require arguments will need the<iomanip>header file. Manipulators that do not require arguments will not need an extra header file. Stream I/O (Deitel, 781–796; Josuttis, 586) Stream Manipulators • Special objects used to format a stream • Used with the stream insertion operator or stream extraction operator cout << setw(10) << right << "Hello" << endl;
Stream I/O (Deitel, 781–796; Josuttis, 586) Some Stream Manipulators
Stream I/O (Deitel, 781–796; Josuttis, 586) Member Functions for Formatting • Streams have member functions that can be used to set their format • Can be used in combination with stream manipulators • Some member functions do the same thing as the manipulators Examples: width(n) • Sets the width of the output to n • Equivalent to the “setw(n)” stream manipulator • cout.width(10); precision(n) • Sets the width of the output to n • Equivalent to the “setprecision(n)” stream manipulator • cout.precision(2);
Bonus Lab 8 Debugging with MS Visual Studio Ford and Teorey, Ch. 5
Debugging (Ford) Debugging Methods • Semantic errors • Logic errors in your code • Examples: code that creates an infinite loop, off-by-one error • Interactive debugger • Can run your program line by line • You can stop program execution at any point and examine the values of its variables
Debugging (Ford) Using the MS Visual StudioInteractive Debugger • Use “Debug” mode • Step Into • Executes the next statement • Will start at the first line in main if there’s no breakpoint • If the next statement is a function call, it will enter the function • The yellow arrow • Points to the next statement that will be executed • Step Over • Executes the next statement • If the next statement is a function call, it will skip over the function • Step Out • Will finish execution of a function and run to the next statement • Breakpoint • The line where you want your program to stop execution • Set by clicking in the margin at the left of the line of code • Variables pane • Displays a list of current variables and their values • Auto, Locals, and Watch tabs
References Deitel, H. M., and P. J. Deitel, C++ How to Program, Fifth Edition. Upper Saddle River, NJ: Prentice Hall, 2005. Ford, A. R., and T. J. Teorey, Practical Debugging in C++. Upper Saddle River, NJ: Prentice Hall, 2002. Josuttis, Nicolai M., The C++ Standard Library, A Tutorial and Reference. Boston: Addison-Wesley, 1999.