Presentation is loading. Please wait.

Presentation is loading. Please wait.

Managing console i/o operations

Similar presentations


Presentation on theme: "Managing console i/o operations"— Presentation transcript:

1 Managing console i/o operations
Dr. Bhargavi Goswami Department of Computer Science Christ University Bangalore.

2 Introduction How to control the way output is printed?
C++ supports rich set of I/O functions and operations. How? C++ uses the concept of stream and stream classes. Application? Terminals, Disks, Tape Drives, etc. This interface is called stream. What is steam? It is a sequence of bytes. It acts as a source (input stream) or destination (output stream). We already have cin and cout. Then why we need this? Cin and Cout is there, but only for screen, not for files. Again, it does not give control of entire screen.

3

4

5 C++ stream classes C++ I/O system has hierarchy of classes to deal with console and disk files. Header file? iosteam. Class ios is Virtual Base class. Why? So that only one copy is inherited by iostream. What are the functions of each class? Lets see next table.

6

7 Unformatted i/o operations
>> is overloaded in istream class. << is overloaded in ostream class. Classes istream and ostream define two member functions get() and put(). We can read and display a line of text more efficiently using line oriented IO functions with getline() and write(). Sytx: cin.getline(line,size); Sytx:cout.write(line,size); Lets see the examples: 10-1-CharacterIOWithGet()AndPut().cpp 10-2-DisplayingStringWithWrite().cpp 10-3-ReadingStringWithGetline.cpp

8 formatted console i/o operation
C++ supports options for formatting output. How? Using… 1) ios class functions and flags 2) Manipulators 3) User defined output functions See next table for functions and further table for manipulators before checking the example programs. In addition to these functions by c++ library, we can create our own user defined and pre-defined formatting functions. Width Function: We need obj to invoke it. W is width of field. Stx: cout.width(w); See the example Program. 10-4-SpecifingFieldSizeWithWidth.cpp.

9

10

11 Continue….. formatted console i/o operation
In C++, by default the numbers are printed with six digits after decimal point. Can we control it? Yes. How? Using precision() member function. Stx: cout.precision(d); where d is number of digits. See the examples: 10-5-PrecisionSettingWithPrecision.cpp 10-6-WidthAndPrecisionManipulators.cpp

12 Continue….. formatted console i/o operation
In C++, unused positions of the field are filled with white spaces by default. Again, to control the filling of white space can be done. How? By using function fill(). Stx: cout.fill(ch); See the example program: 10-7-PaddingWithFill.cpp

13 Continue….. formatted console i/o operation
In C++, if we want to print the text left-justified, what is the option? How do we get a floating point number printed in scientific notation? Ans: set flags using setf() functions. Stx: cout.setf(arg1,arg2); Check the table having Flags with Bit fields. Check the table having Flags without Bit fields. See the following programs: 10-8-ManipulationOfFlagAndBitFields.cpp 10-9-FormatingWithFlagsInSetf.cpp

14 Flags and bit fields for setf() function

15 Flags that do not have bit fields

16 Managing output with manipulators
Does alternate of ios member functions and flags exist? Yes. What is that? Manipulators provided in header file “iomanip”. Then what is the difference between manipulators and ios functions? ios functions return to previous format state. But, manipulators does not return to previous format state. Explicit restore operation has to be performed for manipulators. Check the table having Manipulators. Prog. 10 has manipulators and ios functions also. We can also design our own manipulators. See Prog 11 See the following programs: 10-10-FormatingWithManipulators.cpp 10-11-User-DefinedManipulators.cpp

17 Manipulators with meaning

18 Thank you Next Chapter will start from tomorrow.
Start your preparations now.


Download ppt "Managing console i/o operations"

Similar presentations


Ads by Google