Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.

Slides:



Advertisements
Similar presentations
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
Advertisements

CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
File streams Chapter , ,
I/O and Program Control Statements Dick Steflik. Overloading C++ provides two types of overloading –function overloading the ability to use the same function.
 2006 Pearson Education, Inc. All rights reserved Stream Input/Output.
 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.
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.
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,
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 Chapter 21 - C++ Stream Input/Output Outline 21.1Introduction.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
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.
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.
C++ Programming Certificate University of Washington Cliff Green
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.
C++ Streams Lecture-2.
C++ Lecture 8 Monday, 25 August I/O, File, and Preprocessing l An in-depth review of stream input/output l File handling in C++ l C++ preprocessing.
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.
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:
 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.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 31 Thanks for Lecture Slides: C How to Program by Paul Deital &
 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.
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.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
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:
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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.
CS212: Object Oriented Analysis and Design
Introduction to C++ (Extensions to C)
Chapter 21 - C++ Stream Input/Output Stream Manipulators
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
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.
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Chapter 3: Input/Output
Chapter 12 - C++ Stream Input/Output
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
C++ Stream Input/Output
Chapter 2 part #3 C++ Input / Output
Chapter 12 - C++ Stream Input/Output
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Input/Output Streams, Part 1
Introduction to Programming
Input/Output Streams, Part 2
Presentation transcript:

Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.

I/O Streams C++ uses type-safe I/O, each I/O operation is sensitive to data type. The most basic functions for I/O are in. They are abstracted as stream of data.

I/O Streams Streams are sequences of bytes. In input operations, the bytes flow from a device to main memory. In output operations bytes flow from main memory to a device. Formatted I/O vs. unformatted I/O.

I/O Streams Basic data type for formatted stream. char - for classical I/O stream. Each character is 1 byte. wchar_t for modern I/O stream which takes into account UNICODE. Library functions for formatted streams are in.

I/O Streams Standard I/O templates: Overloading operator for output streams: >> Overloading operator for input streams: <<

I/O Streams Standard I/O Classes:

I/O Streams ios class:

I/O Streams Standard Stream Objects: cin (istream): use extraction operator >>. cout (ostream): use insertion operator <<; cerr (ostream): use to connect with standard error output device. (unbuffered). clog (ostream): similar to cerr but use buffer. buffered vs unbuferred output is whether the output will be delayed. (endl could be used to flush buffers).

I/O Streams Stream Output: - cout use insertion operator <<. - cout.put() cout.put( 'A' ); cout.put( 'A' ).put( '\n' ); cout.put( 65 );

I/O Streams Stream Input: using cin object and extraction operator >>, usually skip blank characters. cin.eof() - return true if encounter a end-of-file character in the stream (Ctrl-Z on Windows, Ctrl-D on Unix/Linux). cin.get() - read next character from the buffer. - read until reaching a delimiter. - read until reaching a maximal number of characters

I/O Streams Stream Input: - Examples

I/O Streams Stream Input: - Examples

I/O Streams Stream Input: cin.getline() similar to cin.get() (version 3) but insert NULL character at the end and peeked the delimiter character out of stream.

I/O Streams Stream Unformatted I/O: - Unformatted I/O is read/write raw bytes directly from/to input/output streams it is used for low level processing of data (where we do not care about data types). cout.write() - write raw bytes to the output stream. cout.read() - read raw bytes from the input stream. cin.gcount() - return the number of characters read from the last input.

I/O Streams Stream Unformatted I/O: - Example

I/O Streams Stream Manipulators : -

I/O Streams Stream Manipulators : -

I/O Streams Stream Manipulators : Integral stream base: - formatting integers (oct, dec, hex, setbase). int number; ….. cout<< std::dect<< number<<" "<<std::oct<<number; cout<<std::setbase(16)<<number;

I/O Streams Stream Manipulators : - Floating-point - precision.

I/O Streams Stream Manipulators : - Set Field Width (width, setw).

I/O Streams Stream Manipulators : - User's defined streams

I/O Streams Stream Manipulators : - User's defined streams

I/O Streams Stream Manipulators : - Justification (left, right)

I/O Streams Stream Manipulators : - Padding (setfill)

I/O Streams Stream Manipulators : - Formatting characters

I/O Streams Stream Error States:

I/O Streams Tying an Output stream to an Input stream: C++ allow to tie an output stream to an input stream to make sure that the output will appear before the corresponding input. cin.tie(&cout); //default by C++ To untie: cin.tie(0);

I/O Streams More Example:

I/O Streams Further Reading: 1. Textbook 1 Chapter Workbook 1 Chapter 12.