Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with ANSI C ++

Similar presentations


Presentation on theme: "Programming with ANSI C ++"— Presentation transcript:

1 Programming with ANSI C ++
A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology

2 Chapter 12 Streams and Formatted IO

3 Streams Pipe like structures with IO specifications
Printers-> only Output Keyboard-> Only Input … Write operation different for Disk and Printer Read Operation different for Disk and Keyboard Device drivers, OS and the consistent interface to all devices

4 Categorizing Streams.. C++ Stream advantage over the C Stream
Object orientation Richer in features Easier to use Two C++ Streams, New and Old Richer IO Different namespace Easier to evolve with third party libraries

5 The hierarchy of C++ stream Classes
ios_base basic_ios <> basic_streambuf basic_istream basic_ostream basic_iostream basic_ofstream basic_ifstream basic_fstream

6 IOS member Functions function The usage width()
It specifies the width for display. precision() It specifies precision of the floating-point number. fill() It specifies the character for filling up unused portion of field. setf() The function specifies format flags that controls output display unsetf() Provide undo operation for above mentioned operations with setf

7 Width() cout.width(10); cout << “C++”;cout <<”Language” then the output will be SSSSSSSC++Language. cout.width(10); cout << “C++”; cout.width(10);cout <<”Language”; then the output will be SSSSSSSC++SSLanguage

8 The Format Flags Flag Name Description skipws
Skipping initial white-space while reading from a stream left Output is left justified right Output is right justified internal Numeric value is padded with space between sign or base (0x, 0 etc) char

9 The Format Flags oct Output to be displayed in octal hex
Flag Name Description oct Output to be displayed in octal hex Output to be displayed in Hex dec Output to be displayed in decimal scientific Output displays floating point values in scientific notation fixed Output is displayed in fixed (non scientific) notation

10 The Format Flags Flag Name Description showbase
Numeric values will be displayed with base character Uppercase In scientific notation E is displayed in Uppercase. showpos Leading + sign before positive values. showpoint Decimal point will be displayed even if it is not present

11 The Format Flags Flag Name Description unibuf
Buffer is flushed after every insertion. boolalpha Boolean values IO as true or false basefield Collection of oct, dec and hex. adjustfield Collection of left, right, and internal field floatfield Collection of scientific and fixed.

12 Two argument Setf Unsetting and then setting is needed for some specific installations of C++ cout.setf(ios::left, ios::right|ios::left|ios::internal) cout.setf(ios::left,ios::adjustfield) is same as previous!

13 Equivalent ios function
Manipulators Manipulators Equivalent ios function setw() width() setprecision() precision() setfill() fill() setiosflags() setf() resetiosflags() unsetf()

14 IOS functions and Manipulators
No return value for Manipulators We can write our own manipulator Ios functions are single and not possible to be combined Manipulators are possible to be applied in chain

15 Comparison continues…
The ios functions need <iostream> while the manipulators need <iomanip> Some manipulators like endl, there is no equivalent ios function Simpler manipulators for setting and unsetting flags are possible

16 Comparison continues…
Manipulators without argument does not need empty parenthesis Pair of manipulators for toggling effect The ios functions are member functions while manipulators are non-member functions

17 Using Manipulators cout<<"see the effect of ios::showpos is to show leading plus sign\n"; cout << setiosflags(ios::showpos)<< 100.0<< endl << resetiosflags(ios::showpos)<<endl; +100 cout << "See the effect of ios::showpoint is showing decimal point\n"; cout << setiosflags(ios::showpoint)<< << endl<< resetiosflags(ios::showpoint)<<endl;

18 Using Manipulators cout <<"See the effect of both of above together\n"; cout << setiosflags(ios::showpoint | ios::showpos)<< << endl<< resetiosflags(ios::showpoint | ios::showpos)<<endl;

19 Using Manipulators cout << "See the effect of both uppercase and scientific together\n"; cout << setiosflags(ios::uppercase | ios::scientific)<< <<endl << resetiosflags(ios::uppercase | ios::scientific)<<endl; E+002

20 Using Manipulators cout << "See how printed with default right alignment\n"; cout << "and then see how it changes after left alignment\n"; cout<< setw(20)<< <<endl << setiosflags(ios::left)<<setw(20)<< <<endl << resetiosflags(ios::left)<<endl;

21 Using Manipulators cout<<"See the effect of using internal which displays the sign in the left and right justifies the output, thus filling spaces in between\n"; cout << setiosflags(ios::internal|ios::showpos) <<setw(20)<< <<endl;

22 Using Manipulators cout<< "Now see the effect of fill char with internal\n"; cout<<setw(20)<< setfill('*')<< <<endl << resetiosflags(ios::internal|ios::showpos)<< setfill(' ')<<endl; +************

23 Using Manipulators cout<<"See how truthfulness is printed as 1 by default, and changed to true when boolalpha is set with cout\n"; bool Test=true; cout << Test << endl; // this will display 1 cout << setiosflags(ios::boolalpha); cout << Test<<endl; // this will display true

24 Using Manipulators cout<<"See how truthfulness is read as 0 or 1 when boolalpha is set with cin\n"; cout<<"Please enter true or false\n"; cin >> setiosflags(ios::boolalpha) ; cin>>Test; cout<< Test<<endl; } Please enter true or false false

25 Toggle using Manipulator
bool Test = false; cout << boolalpha << Test << endl; cout << noboolalpha << Test<< endl; false 0 cout << showpoint << << endl; cout << noshowpoint << << endl;

26 Toggle using Manipulator
cout << showpos << << endl; cout << noshowpos << << endl; cout << showpoint << uppercase << scientific << <<endl; cout << nouppercase << << endl; E e+002

27 Shorthand Manipulators
setiosflags(ios::fixed) can be written as fixed setiosflags(ios::left) can be written as left setiosflags(ios::showpoint | ios::showpos) can be written as showpoint << showpos

28 Creating our own manipulators
ostream & PrintHeading(ostream & TempOut) { TempOut<<setw(80)<< setiosflags(ios::left); TempOut << "GLS Higher Secondary School"<<endl <<setw(80)<< "Standard 12th"<<endl; return TempOut; } We have to call cout << PrintHeading to print above heading

29 Creating our own manipulators
ostream & PrintLine(ostream & TempOut) { TempOut << " "; TempOut << endl; return TempOut; } This will print a line. We have to call it like cout << PrintLine; to draw this line

30 Importance of reference
cout << PrintHeading << PrintLine << PrintMarksheetHeading will not be possible if the reference is not returned If the reference is not passed, the argument (ostream & TempOut) TempOut is alias of cout and thus changes made in TempOut is actually made to cout and when TempOut is returned back, it is the cout which is returned back

31 Using a user defined function
void FormatPrint(int TempRollNo, string TempName, float TempMarks) { cout << setw(15)<< TempRollNo << setw(15)<< TempName<< setw(10)<< setprecision(2)<< TempMarks<<endl; } FormatPrint(1,"RamChandra", 275.0); FormatPrint(2,"Backham", 275.0);


Download ppt "Programming with ANSI C ++"

Similar presentations


Ads by Google