1 / 30

Chapter 10 Console I/O Operations

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 <<

kemal
Download Presentation

Chapter 10 Console I/O Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. §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

  4. 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

  5. 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;

  6. 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

  7. §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

  8. 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

  9. put() and get() int get(); istream& get (char& c); ostream& put (char c); • get()/put(): member of istream/ostream • Using : 9

  10. put() and get() Example: 10

  11. 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

  12. 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

  13. Example of getline() cin.getline(city, size); 13

  14. 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.

  15. Example of write()

  16. §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

  17. Defining Field Width: width() • Program 10.4 17

  18. 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

  19. Example of precision() (example 10.5) 19

  20. 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

  21. Format Flags, Bit-field and setf() 21

  22. Example of setf(arg1, arg2) 22

  23. 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

  24. Displaying Trailing Zeros and Plus Sign 24

  25. Example of setf(arg) • Program 10.7 25

  26. §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

  27. Example of Manipulators Program 10.8 27

  28. Defining Our Own Manipulators 28

  29. 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

  30. Review Questions • Null.

More Related