Programming with ANSI C ++

Slides:



Advertisements
Similar presentations
CPS120: Introduction to Computer Science INPUT/OUTPUT.
Advertisements

CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
File streams Chapter , ,
I/O Streams as an Introduction to Objects and Classes
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
Programming Stream Manipulators. COMP102 Prog. Fundamentals I: Stream Manipulator Slide 2 Stream Manipulators in iostream.h Library I/O stream manipulators.
 2006 Pearson Education, Inc. All rights reserved Stream Input/Output.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
計算機概論實習 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction.
CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.
Chapter 3: Input/Output
Namespaces, I/O streams. Namespaces Namespace – what’s this? Namespace – what’s this?namespace When do we need the namespace? When do we need the namespace?
Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
1 Advanced Input and Output COSC1567 C++ Programming Lecture 9.
FORMATTED INPUT OUTPUT. Topics to be discussed……………….. Formatted Console I/O operationsFormatted Console I/O operations Defining field width :Width()Defining.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 12 - C++ Stream Input/Output Outline 12.1 Introduction 12.2 Streams Classic Streams vs.
Output Formatting. Precision #include... float grade = f; cout.precision(4); cout
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Definition Various stream manipulators can be used to specify the kinds of formatting to be performed during stream-I/O operations. Stream manipulators.
C++ Streams Lecture-2.
Output Formatting No, I don't want 6 digits…. Standard Behavior Rules for printing decimals: – No decimal point: prints as 1 – No trailing zeros:
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
C++ Streams Lecture-2. C++ Streams Stream  A transfer of information in the form of a sequence of bytes I/O Operations:  Input stream: A stream that.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
The C++ Programming Language Streams. Contents u Output Stream u Input Stream u Formatting u Manipulators u Files & Streams.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
Student Book Input / Output in C++
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital &
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
#include int main() { char *string1=”C++”; char *string2=”Program”; int m=strlen(string1); int n=strlen(string2); for(int i=1;i
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
C++ How to Program, 8/e © Copyright by Pearson Education, Inc. All Rights Reserved.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 13: Exam 1 Preview.
Chapter 13 Stream Input/Output C++ How to Program, 9/e © Copyright 2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 3 L7.
TMF1414 Introduction to Programming
Chapter 21 - C++ Stream Input/Output Stream Manipulators
Output Stream Formatting
Chapter 21 - C++ Stream Input/Output
Chapter 2 part #3 C++ Input / Output
Formatting Screen Input/Output
Standard Input/Output Streams
Standard Input/Output Streams
Advanced Input and Output
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Chapter 5 Input and Output Streams
Chapter 12 - C++ Stream Input/Output
Managing console i/o operations
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Chapter 3: Expressions and Interactivity
Chapter 4 INPUT AND OUTPUT OBJECTS
Chapter 2 part #3 C++ Input / Output
Chapter 12 - C++ Stream Input/Output
Presentation transcript:

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