CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital &

Slides:



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

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.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
計算機概論實習 Integral Stream Base expression: dec, oct, hex, setbase, and showbase Use header Integers normally base 10 (decimal) Stream manipulators.
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 3: Input/Output
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction 21.2Streams Iostream Library Header.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
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.
C++ Programming Certificate University of Washington Cliff Green
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
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 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
CHAPTER 3 INPUT/OUTPUT. In this chapter, you will:  Learn what a stream is and examine input and output streams  Explore how to read data from the standard.
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
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.
1 Streams In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
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.
CPS120: Introduction to Computer Science Formatted I/O.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 11- C++ Stream Input/Output Outline 11.1Introduction 11.2Streams Iostream Library Header.
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
Student Book Input / Output in C++
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
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.
1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,
Chapter 3: Input/Output
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.
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.
C++ How to Program, 8/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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 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.
Topic 2 Input/Output.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Chapter 21 - C++ Stream Input/Output Stream Manipulators
Output Stream Formatting
Chapter 21 - C++ Stream Input/Output
Formatting Screen Input/Output
Standard Input/Output Streams
Standard Input/Output Streams
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 12 - C++ Stream Input/Output
Chapter 4 INPUT AND OUTPUT OBJECTS
Programming with ANSI C ++
C++ Stream Input/Output
Chapter 12 - C++ Stream Input/Output
Input/Output Streams, Part 2
Presentation transcript:

CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital & Harvey Deital 6Ed.

iostream …… Continued

istream Member Functions peek, putback and ignore ignore member function – Skips over a designated number of characters (default of one) – Terminates upon encountering a designated delimiter (default is EOF, skips to the end of the file) putback member function – Places the previous character obtained by get back in to the stream. peek – Returns the next character from the stream without removing it CSC 141 Introduction to computer Programming 3

Type-Safe I/O > operators –Overloaded to accept data of different types –When unexpected data encountered, error flags set –Program stays in control CSC 141 Introduction to computer Programming 4

Unformatted I/O with read, gcount and write read and write member functions –Unformatted I/O –Input/output raw bytes to or from a character array in memory –Since the data is unformatted, the functions will not terminate at a newline character for example Instead, like getline, they continue to process a designated number of characters –If fewer than the designated number of characters are read, then the failbit is set gcount : –Returns the total number of characters read in the last input operation CSC 141 Introduction to computer Programming 5

Enter a sentence: Using the read, write and gcount member functions The sentence entered was: Using the read, writ Output

Stream Manipulators Stream manipulator capabilities –Setting field widths –Setting precisions –Setting and unsetting format flags –Setting the fill character in fields –Flushing streams –Inserting a newline in the output stream and flushing the stream –Inserting a null character in the output stream and skipping whitespace in the input stream CSC 141 Introduction to computer Programming 7

Integral Stream Base: dec, oct, hex and setbase oct, hex or dec : –Change base of which integers are interpreted from the stream. Example: int n = 15; cout << hex << n; –Prints "F" setbase : –Changes base of integer output –Load –Accepts an integer argument ( 10, 8, or 16 ) cout << setbase(16) << n; –Parameterized stream manipulator - takes an argument CSC 141 Introduction to computer Programming 8

Enter a decimal number: in hexadecimal is: in octal is: in decimal is: 20 Output

Floating-Point Precision ( precision, setprecision ) precision –Member function –Sets number of digits to the right of decimal point cout.precision(2); – cout.precision() returns current precision setting setprecision –Parameterized stream manipulator –Like all parameterized stream manipulators, required –Specify precision: cout << setprecision(2) << x; For both methods, changes last until a different value is set CSC 141 Introduction to computer Programming 11

Square root of 2 with precisions 0-9. Precision set by the precision member function: Precision set by the setprecision manipulator: Output

Field Width( setw, width ) ios width member function – Sets field width (number of character positions a value should be output or number of characters that should be input) – Returns previous width – If values processed are smaller than width, fill characters inserted as padding – Values are not truncated - full number printed – cin.width(5); setw stream manipulator cin >> setw(5) >> string; Remember to reserve one space for the null character CSC 141 Introduction to computer Programming 15

Enter a sentence: This is a test of the width member function This is a test of the widt h memb er func tion Output

User-Defined Manipulators We can create our own stream manipulators – bell – ret (carriage return) – tab – endLine Parameterized stream manipulators –Consult installation manuals CSC 141 Introduction to computer Programming 18

Testing the tab manipulator: a b c Testing the ret and bell manipulators: Output

Stream Format States Format flags – Specify formatting to be performed during stream I/O operations setf, unsetf and flags – Member functions that control the flag settings CSC 141 Introduction to computer Programming 21

Format State Flags – Defined as an enumeration in class ios – Can be controlled by member functions – flags - specifies a value representing the settings of all the flags Returns long value containing prior options – setf - one argument, "ors" flags with existing flags – unsetf - unsets flags – setiosflags - parameterized stream manipulator used to set flags – resetiosflags - parameterized stream manipulator, has same functions as unsetf Flags can be combined using bitwise OR ( | ) CSC 141 Introduction to computer Programming 22

Format State Flags CSC 141 Introduction to computer Programming 23

Trailing Zeros and Decimal Points ( ios::showpoint ) ios::showpoint –Forces a float with an integer value to be printed with its decimal point and trailing zeros cout.setf(ios::showpoint) cout << 79; 79 will print as Number of zeros determined by precision settings CSC 141 Introduction to computer Programming 24

Before setting the ios::showpoint flag prints as: prints as: prints as: 9 After setting the ios::showpoint flag prints as: prints as: prints as: Output

Justification ( ios::left, ios::right, ios::internal ) ios::left –Fields to left-justified with padding characters to the right ios::right –Default setting –Fields right-justified with padding characters to the left Character used for padding set by – fill member function – setfill parameterized stream manipulator –Default character is space CSC 141 Introduction to computer Programming 27

Justification ( ios::left, ios::right, ios::internal ) internal flag –Number’s sign left-justified –Number’s magnitude right-justified –Intervening spaces padded with the fill character static data member ios::adjustfield –Contains left, right and internal flags – ios::adjustfield must be the second argument to setf when setting the left, right or internal justification flags cout.setf( ios::left, ios::adjustfield); CSC 141 Introduction to computer Programming 28

Default is right justified: USING MEMBER FUNCTIONS Use setf to set ios::left: Use unsetf to restore default: USING PARAMETERIZED STREAM MANIPULATORS Use setiosflags to set ios::left: Use resetiosflags to restore default: Output

+ 123 Output

Padding ( fill, setfill ) fill member function –Specifies the fill character –Space is default –Returns the prior padding character cout.fill('*'); setfill manipulator –Also sets fill character cout << setfill ('*'); CSC 141 Introduction to computer Programming 32

10000 printed as int right and left justified and as hex with internal justification. Using the default pad character (space): x 2710 Using various padding characters: ***** %%% 0x^^^^2710 Output

Integral Stream Base ( ios::dec, ios::oct, ios::hex, ios::showbase ) ios::basefield static member –Used similarly to ios::adjustfield with setf –Includes the ios::oct, ios::hex and ios::dec flag bits –Specify that integers are to be treated as octal, hexadecimal and decimal values –Default is decimal –Default for stream extractions depends on form inputted Integers starting with 0 are treated as octal Integers starting with 0x or 0X are treated as hexadecimal –Once a base specified, settings stay until changed CSC 141 Introduction to computer Programming 36

Printing integers preceded by their base: x64 Output

Floating-Point Numbers; Scientific Notation (ios::scientific, ios::fixed) ios::scientific –Forces output of a floating point number in scientific notation: e+009 ios::fixed –Forces floating point numbers to display a specific number of digits to the right of the decimal (specified with precision ) CSC 141 Introduction to computer Programming 39

Floating-Point Numbers; Scientific Notation (ios::scientific, ios::fixed) static data member ios::floatfield –Contains ios::scientific and ios::fixed –Used similarly to ios::adjustfield and ios::basefield in setf cout.setf(ios::scientific, ios::floatfield); – cout.setf(0, ios::floatfield) restores default format for outputting floating-point numbers CSC 141 Introduction to computer Programming 40

Displayed in default format: e+009 Displayed in scientific format: e e+009 Displayed in default format after unsetf: e+009 Displayed in fixed format: Output

Uppercase/Lowercase Control ( ios::uppercase ) ios::uppercase – Forces uppercase E to be output with scientific notation 4.32E+010 – Forces uppercase X to be output with hexadecimal numbers, and causes all letters to be uppercase 75BDE CSC 141 Introduction to computer Programming 43

Printing uppercase letters in scientific notation exponents and hexadecimal values: 4.345E BCD15 Output

Setting and Resetting the Format Flags ( flags, setiosflags, resetiosflags ) flags member function –Without argument, returns the current settings of the format flags (as a long value) –With a long argument, sets the format flags as specified Returns prior settings setf member function –Sets the format flags provided in its argument –Returns the previous flag settings as a long value –Unset the format using unsetf member function long previousFlagSettings = cout.setf( ios::showpoint | ios::showpos ); CSC 141 Introduction to computer Programming 45

setf with two long arguments cout.setf( ios::left, ios::adjustfield ); clears the bits of ios::adjustfield then sets ios::left –This version of setf can be used with – ios::basefield (ios::dec, ios::oct, ios::hex) – ios::floatfield (ios::scientific, ios::fixed) – ios::adjustfield (ios::left, ios::right, ios::internal ) unsetf –Resets specified flags –Returns previous settings Setting and Resetting the Format Flags (flags, setiosflags, resetiosflags) CSC 141 Introduction to computer Programming 46

The value of the flags variable is: 513 Print int and double in original format: The value of the flags variable is: Print int and double in a new format specified using the flags member function: e-002 The value of the flags variable is: 513 Print values in original format again: Output

Stream Error States eofbit –Set for an input stream after end-of-file encountered – cin.eof() returns true if end-of-file has been encountered on cin failbit – Set for a stream when a format error occurs – cin.fail() - returns true if a stream operation has failed – Normally possible to recover from these errors CSC 141 Introduction to computer Programming 49

Stream Error States badbit –Set when an error occurs that results in data loss – cin.bad() returns true if stream operation failed – normally nonrecoverable goodbit – Set for a stream if neither eofbit, failbit or badbit are set – cin.good() returns true if the bad, fail and eof functions would all return false. – I/O operations should only be performed on “good” streams rdstate –Returns the state of the stream –Stream can be tested with a switch statement that examines all of the state bits –Easier to use eof, bad, fail, and good to determine state CSC 141 Introduction to computer Programming 50

Stream Error States clear –Used to restore a stream’s state to “good” – cin.clear() clears cin and sets goodbit for the stream – cin.clear( ios::failbit ) actually sets the failbit Might do this when encountering a problem with a user- defined type Other operators – operator! Returns true if badbit or failbit set – operator void* Returns false if badbit or failbit set –Useful for file processing CSC 141 Introduction to computer Programming 51

Before a bad input operation: cin.rdstate(): 0 cin.eof(): 0 cin.fail(): 0 cin.bad(): 0 cin.good(): 1 Expects an integer, but enter a character: A After a bad input operation: cin.rdstate(): 2 cin.eof(): 0 cin.fail(): 1 cin.bad(): 0 cin.good(): 0 After cin.clear() cin.fail(): 0 cin.good(): 1 Output

Tying an Output Stream to an Input Stream tie member function –Synchronize operation of an istream and an ostream –Outputs appear before subsequent inputs –Automatically done for cin and cout inputStream.tie( &outputStream ); –Ties inputStream to outputStream – cin.tie( &cout) done automatically inputStream.tie( 0 ); –Unties inputStream from an output stream CSC 141 Introduction to computer Programming 54