 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams 21.2.1Iostream Library.

Slides:



Advertisements
Similar presentations
 2006 Pearson Education, Inc. All rights reserved Stream Input/Output.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers in C++; Section 3.5; Chapter 5 Concept of pointers absent in Java Pointer holds a memory address.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
計算機概論實習 Stream Stream: sequence of bytes Input: from device (keyboard, disk drive) to memory Output: from memory to device (monitor, printer,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction.
 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.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
C++ Programming Certificate University of Washington Cliff Green
DCS 5085 C++ Fundamentals Chapter 4. Overview Basics of a typical C++ Environment C++ Stream Input/Output Classic Stream vs. Standard Stream Iostream.
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.
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
Week 1 Algorithmization and Programming Languages.
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.
Chapter 14 Stream Input and Output By C. Shing ITEC Dept Radford University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 11- C++ Stream Input/Output Outline 11.1Introduction 11.2Streams Iostream Library Header.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
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.
1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 25 December 1, 2009.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
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++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Chapter 13 Stream Input/Output C++ How to Program, 9/e © Copyright 2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.
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.
Chapter 15 - C++ As A "Better C"
Chapter 1.2 Introduction to C++ Programming
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basic Input and Output Operations
Chapter 21 - C++ Stream Input/Output
Chapter 2 part #3 C++ Input / Output
Standard Input/Output Streams
Standard Input/Output Streams
Operator Overloading.
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 3: Input/Output
Chapter 12 - C++ Stream Input/Output
Introduction to C++ Programming
C++ Stream Input/Output
Chapter 15 - C++ As A "Better C"
Capitolo 1 – Introduction C++ Programming
Chapter 2 part #3 C++ Input / Output
C++ Programming Lecture 20 Strings
Chapter 12 - C++ Stream Input/Output
Chapter 1 c++ structure C++ Input / Output
Input/Output Streams, Part 1
Introduction to Programming
Presentation transcript:

 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library Header Files Stream Input/Output Classes and Objects 21.3Stream Output Stream-Insertion Operator Cascading Stream-Insertion/Extraction Operators Output of char * Variables Character Output with Member Function put ; Cascading put s 21.4Stream Input Stream-Extraction Operator get and getline Member Functions istream Member Functions peek, putback and ignore Type-Safe I/O 21.5Unformatted I/O with read, gcount and write

 2000 Prentice Hall, Inc. All rights reserved Introduction Many C++ I/O features are object-oriented –Use references, function overloading and operator overloading C++ uses type safe I/O –Each I/O operation is automatically performed in a manner sensitive to the data type Extensibility –Users may specify I/O of user-defined types as well as standard types

 2000 Prentice Hall, Inc. All rights reserved Streams Stream –A transfer of information in the form of a sequence of bytes I/O Operations: –Input: A stream that flows from an input device ( i.e.: keyboard, disk drive, network connection) to main memory –Output: A stream that flows from main memory to an output device ( i.e.: screen, printer, disk drive, network connection)

 2000 Prentice Hall, Inc. All rights reserved Streams (II) I/O operations are a bottleneck –The time for a stream to flow is many times larger than the time it takes the CPU to process the data in the stream Low-level I/O –Unformatted –Individual byte unit of interest –High speed, high volume, but inconvenient for people High-level I/O –Formatted –Bytes grouped into meaningful units: integers, characters, etc. –Good for all I/O except high-volume file processing

 2000 Prentice Hall, Inc. All rights reserved Iostream Library Header Files iostream library: – : Contains cin, cout, cerr, and clog objects – : Contains parameterized stream manipulators – : Contains information important to user- controlled file processing operations

 2000 Prentice Hall, Inc. All rights reserved Stream Input/Output Classes and Objects ios: –istream and ostream inherit from ios iostream inherits from istream and ostream. << (left-shift operator) –Overloaded as stream insertion operator >> (right-shift operator) –Overloaded as stream extraction operator –Both operators used with cin, cout, cerr, clog, and with user-defined stream objects

 2000 Prentice Hall, Inc. All rights reserved Stream Input/Output Classes and Objects (II) istream: input streams cin >> someVariable ; cin knows what type of data is to be assigned to someVariable (based on the type of someVariable ). ostream: output streams –cout << someVariable; cout knows the type of data to output –cerr << someString; Unbuffered - prints someString immediately. –clog << someString; Buffered - prints someString as soon as output buffer is full or flushed

 2000 Prentice Hall, Inc. All rights reserved Stream Output ostream : performs formatted and unformatted output –Uses put for characters and write for unformatted characters –Output of numbers in decimal, octal and hexadecimal –Varying precision for floating points –Formatted text outputs

 2000 Prentice Hall, Inc. All rights reserved Stream-Insertion Operator << is overloaded to output built-in types –Can also be used to output user-defined types –cout << ‘\n’; Prints newline character –cout << endl; endl is a stream manipulator that issues a newline character and flushes the output buffer –cout << flush; flush flushes the output buffer

 2000 Prentice Hall, Inc. All rights reserved Cascading Stream- Insertion/Extraction Operators << : Associates from left to right, and returns a reference to its left-operand object (i.e. cout ). –This enables cascading cout << "How" << " are" << " you?"; Make sure to use parenthesis: cout << "1 + 2 = " << (1 + 2); NOT cout << "1 + 2 = " << 1 + 2;

 2000 Prentice Hall, Inc. All rights reserved Output of char * Variables << will output a variable of type char * as a string To output the address of the first character of that string, cast the variable as type void *

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. Initialize string 2. Print string 2.1 cast into void * 2.2 Print value of pointer (address of string) Program Output 1// Fig. 21.8: fig21_08.cpp 2// Printing the address stored in a char* variable 3#include 4 5using std::cout; 6using std::endl; 7 8int main() 9{9{ 10 char *string = "test"; cout << "Value of string is: " << string 13 ( string ) is: " 14 ( string ) << endl; 15 return 0; 16} Value of string is: test Value of static_cast ( string ) is: 0046C070

 2000 Prentice Hall, Inc. All rights reserved Character Output with Member Function put ; Cascading put s put member function –Outputs one character to specified stream cout.put( 'A'); –Returns a reference to the object that called it, so may be cascaded cout.put( 'A' ).put( '\n' ); –May be called with an ASCII-valued expression cout.put( 65 ); Outputs A

 2000 Prentice Hall, Inc. All rights reserved Stream Input >> (stream-extraction) –Used to perform stream input –Normally ignores whitespaces (spaces, tabs, newlines) –Returns zero ( false ) when EOF is encountered, otherwise returns reference to the object from which it was invoked (i.e. cin ) This enables cascaded input cin >> x >> y; >> controls the state bits of the stream –failbit set if wrong type of data input –badbit set if the operation fails

 2000 Prentice Hall, Inc. All rights reserved Stream-Extraction Operator >> and << have relatively high precedence –Conditional and arithmetic expressions must be contained in parentheses Popular way to perform loops while (cin >> grade) Extraction returns 0 ( false ) when EOF encountered, and loop ends

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. Initialize variables 2. Perform loop 3. Output Program Output 1// Fig : fig21_11.cpp 2// Stream-extraction operator returning false on end-of-file. 3#include 4 5using std::cout; 6using std::cin; 7using std::endl; 8 9int main() 10{ 11 int grade, highestGrade = -1; cout << "Enter grade (enter end-of-file to end): "; 14 while ( cin >> grade ) { 15 if ( grade > highestGrade ) 16 highestGrade = grade; cout << "Enter grade (enter end-of-file to end): "; 19 } cout << "\n\nHighest grade is: " << highestGrade << endl; 22 return 0; 23} Enter grade (enter end-of-file to end): 67 Enter grade (enter end-of-file to end): 87 Enter grade (enter end-of-file to end): 73 Enter grade (enter end-of-file to end): 95 Enter grade (enter end-of-file to end): 34 Enter grade (enter end-of-file to end): 99 Enter grade (enter end-of-file to end): ^Z Highest grade is: 99

 2000 Prentice Hall, Inc. All rights reserved get and getline Member Functions cin.get(): inputs a character from stream (even white spaces) and returns it cin.get( c ): inputs a character from stream and stores it in c

 2000 Prentice Hall, Inc. All rights reserved get and getline Member Functions (II) cin.get(array, size): –Accepts 3 arguments: array of characters, the size limit, and a delimiter ( default of ‘\n’ ). –Uses the array as a buffer –When the delimiter is encountered, it remains in the input stream –Null character is inserted in the array –Unless delimiter flushed from stream, it will stay there cin.getline(array, size) –Operates like cin.get(buffer, size) but it discards the delimiter from the stream and does not store it in array –Null character inserted into array

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. Initialize variables 2. Input data 2.1 Function call 3. Output Program Output 1// Fig : fig21_12.cpp 2// Using member functions get, put and eof. 3#include 4 5using std::cout; 6using std::cin; 7using std::endl; 8 9int main() 10{ 11 char c; cout << "Before input, cin.eof() is " << cin.eof() 14 << "\nEnter a sentence followed by end-of-file:\n"; while ( ( c = cin.get() ) != EOF ) 17 cout.put( c ); cout << "\nEOF in this system is: " << c; 20 cout << "\nAfter input, cin.eof() is " << cin.eof() << endl; 21 return 0; 22} Before input, cin.eof() is 0 Enter a sentence followed by end-of-file: Testing the get and put member functions^Z Testing the get and put member functions EOF in this system is: -1 After input cin.eof() is 1

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. Initialize variables 2. Input 2.1 Function call 3. Output Program Output 1// Fig : fig21_14.cpp 2// Character input with member function getline. 3#include 4 5using std::cout; 6using std::cin; 7using std::endl; 8 9int main() 10{ 11 const SIZE = 80; 12 char buffer[ SIZE ]; cout << "Enter a sentence:\n"; 15 cin.getline( buffer, SIZE ); cout << "\nThe sentence entered is:\n" << buffer << endl; 18 return 0; 19} Enter a sentence: Using the getline member function The sentence entered is: Using the getline member function

 2000 Prentice Hall, Inc. All rights reserved 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

 2000 Prentice Hall, Inc. All rights reserved Type-Safe I/O > operators –Overloaded to accept data of different types –When unexpected data encountered, error flags set –Program stays in control

 2000 Prentice Hall, Inc. All rights reserved 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

 2000 Prentice Hall, Inc. All rights reserved. Outline 1. Initialize objects 2. Input 3. Output Program Output 1// Fig : fig21_15.cpp 2// Unformatted I/O with read, gcount and write. 3#include 4 5using std::cout; 6using std::cin; 7using std::endl; 8 9int main() 10{ 11 const int SIZE = 80; 12 char buffer[ SIZE ]; cout << "Enter a sentence:\n"; 15 cin.read( buffer, 20 ); 16 cout << "\nThe sentence entered was:\n"; 17 cout.write( buffer, cin.gcount() ); 18 cout << endl; 19 return 0; 20} Enter a sentence: Using the read, write, and gcount member functions The sentence entered was: Using the read, writ