810 likes | 2.05k Views
Chapter 10 Console I/O Operations. §10.1 C++ Stream & C++ Stream Classes §10.2 Unformatted I/O Operations §10.3 Formatted Console I/O Operations §10.4 Managing Output with Manipulators. Input and Output?. How to provide the input data & present results? cin >> and cout <<
E N D
Chapter 10Console I/O Operations §10.1 C++ Stream & C++ Stream Classes §10.2 Unformatted I/O Operations §10.3 Formatted Console I/O Operations §10.4 Managing Output with Manipulators
Input and Output? How to provide the input data & present results? cin >> and cout << How to control the format of input and output? C++ supports a rich set of I/O functions and operations C++ supports all of C’s rich set of I/O functions These functions and operations use the advanced features of C++: classes, inheritance, polymorphism Using stream and stream classes to implement its I/O operations with the console (this chapter), and with disk files(next chapter). 2
§10.1 C++ Stream Stream: an interface to operate on different I/O systems Display/terminals, hard disks, flash disks, etc… Independent of the actual device A stream is a sequence of bytes A program extracts the bytes from an input stream and inserts bytes into an output stream Input stream Extract Input device Program Output stream Output device Insert 3
About C++ Stream A C++ program handles data (input/output) independent of the devices used. The data in the input stream can come from the keyboard or any other storage device. The data in the output can go to the screen or any other storage device : 4
The cin/cout stream • Pre-defined streams • Automatically opened when a program begins its execution. • Standard input/output stream • Connected to the standard input/output devices • Usually the keyboard/screen. • Can redirect streams to other devices or files freopen("test.txt", "r", stdin); freopen("test.txt", "w", stdout); cout<<“Test it.”<<endl;
C++ Stream Classes ios istream ostream streambuf get(), read(), getline() overloading << put(), write() overloading >> iostream istream_withassign iostream_withassign ostream_withassign cin cout • Stream classes for console I/O operations: • These classes are declared in the header file iostream: • Eg: include <iostream> • ios is declared as the virtual base class
§10.2 Unformatted I/O Operations • Using cin/cout, an istream/ostream object • Operators >> and << • Overloaded in istream/ostream to recognize all the basic C++ types. • General format for cin and cout: 7
Notes on >> • >> will cause the computer to stop the execution and look for input data from the standard input device. • Input data are separated by white spaces and should match the type of variable in cin list. • Spaces , newlines and tabs will be skipped. • The reading for a variable will be terminated at • a white space OR • a character that does not match the destination type. 8
put() and get() int get(); istream& get (char& c); ostream& put (char c); • get()/put(): member of istream/ostream • Using : 9
put() and get() Example: 10
put() and get() The text is sent to the program as soon as we press the RETURN key. The program then reads and displays characters one by one. the process is terminated when the newline character is encountered. Example: 11
getline() • cin.getline(line, size) • reads character input into the variable line. • the reading is terminated as soon as either ‘\n’ is encountered or size-1 characters are read. • the newline character ‘\n’ is read but not saved. Instead, it is replaced by the null character. 12
Example of getline() cin.getline(city, size); 13
write() • cout.write(line, size) • displays an entire line • line: the string to be displayed • size: the number of character to display • Note: it does not stop automatically at the null character • If the size is greater than the length of line, then it displays beyond the bounds of line.
§10.3 Formatted Console I/O Operation • To format the output: • ios class functions and flags • Manipulators • User-defined output functions • ios format functions 16
Defining Field Width: width() • Program 10.4 17
Setting precision: precision() • To specify the number of digits to be displayed after the decimal point • 6 digits after the decimal point, by default 18
Example of precision() (example 10.5) 19
Filling and Padding: fill() cout.fill('<'); cout.precision(3); for(int n=1; n<=6; n++){ cout.width(5); cout << n; cout.width(10); cout << 1.0 / float(n) << "\n"; if (n==3) cout.fill('>'); } cout << "\nPadding changed \n\n"; cout.fill('#'); cout.width(15); cout << 12.345678 << "\n"; • example : Program 10.6 20
Displaying Trailing Zeros and Plus Sign • To print 10.75, 25.00 and 15.50 using a field width of 8 positions, with 2 digits precision: • How to show: 23
Example of setf(arg) • Program 10.7 25
§10.4 Output with Manipulators • Manipulators • Also functions to format output • Special: can be included in the I/O statements • Defined in header file iomanip 26
Example of Manipulators Program 10.8 27
A Summary • Concepts of stream, stream classes • Unformatted input and output • >>, <<, cin, cout, get, put, getline, write • Formatting output • Format functions • width, precision, setf, etc. • Manipulators • Standard from C++ library • Defined by user
Review Questions • Null.