Programming with ANSI C ++ A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Chapter 12 Streams and Formatted IO
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
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
The hierarchy of C++ stream Classes ios_base basic_ios <> basic_streambuf basic_istream basic_ostream basic_iostream basic_ofstream basic_ifstream basic_fstream
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
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
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
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
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
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.
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!
Equivalent ios function Manipulators Manipulators Equivalent ios function setw() width() setprecision() precision() setfill() fill() setiosflags() setf() resetiosflags() unsetf()
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
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
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
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)<< 100.0 << endl<< resetiosflags(ios::showpoint)<<endl; 100.000
Using Manipulators cout <<"See the effect of both of above together\n"; cout << setiosflags(ios::showpoint | ios::showpos)<< 100.0 << endl<< resetiosflags(ios::showpoint | ios::showpos)<<endl; +100.000
Using Manipulators cout << "See the effect of both uppercase and scientific together\n"; cout << setiosflags(ios::uppercase | ios::scientific)<< 100.123<<endl << resetiosflags(ios::uppercase | ios::scientific)<<endl; 1.001230E+002
Using Manipulators cout << "See how 100.123 printed with default right alignment\n"; cout << "and then see how it changes after left alignment\n"; cout<< setw(20)<< 100.123<<endl << setiosflags(ios::left)<<setw(20)<< 100.123<<endl << resetiosflags(ios::left)<<endl;
Using Manipulators 100.123 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)<< 100.123<<endl; + 100.123
Using Manipulators cout<< "Now see the effect of fill char with internal\n"; cout<<setw(20)<< setfill('*')<< 100.123<<endl << resetiosflags(ios::internal|ios::showpos)<< setfill(' ')<<endl; +************100.123
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
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
Toggle using Manipulator bool Test = false; cout << boolalpha << Test << endl; cout << noboolalpha << Test<< endl; false 0 cout << showpoint << 100.00 << endl; cout << noshowpoint << 100.00 << endl; 100.000 100
Toggle using Manipulator cout << showpos << 100.00 << endl; cout << noshowpos << 100.00 << endl; +100 100 cout << showpoint << uppercase << scientific << 100.20<<endl; cout << nouppercase << 100.20 << endl; 1.002000E+002 1.002000e+002
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
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
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
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
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);