Chapter 14 Stream Input and Output By C. Shing ITEC Dept Radford University.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

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.
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.
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
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.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf.
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.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
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.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
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.
Chapter 3 Flow Control By C. Shing ITEC Dept Radford University.
Lecture #5: iostream functions دوال الإدخال والإخراج Dr. Hmood Al-Dossari King Saud University Department of Computer Science 4 March 2012.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
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:
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.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
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
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.
Chapter 16 C++ Strings By C. Shing ITEC Dept Radford University.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Input/Output. Objectives In this chapter you will: Learn what a stream is and examine input and output streams Explore how to use the input stream functions.
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.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Chapter 8 Bit Operations By C. Shing ITEC Dept Radford University.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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
Topic 2 Input/Output.
Introduction to C++ (Extensions to C)
CPS120: Introduction to Computer Science
Output Stream Formatting
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
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
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.
Programming with Data Files
Chapter 3: Input/Output
Managing console i/o operations
Chapter 3 Input output.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Chapter 4 INPUT AND OUTPUT OBJECTS
Chapter 2 part #3 C++ Input / Output
Input/Output Streams, Part 1
Input/Output Streams, Part 2
Programming Fundamental-1
Presentation transcript:

Chapter 14 Stream Input and Output By C. Shing ITEC Dept Radford University

Slide 2 Objectives Understand how to use unformatted input (cin) and output (cout) Understand how to use formatted input (cin) and output (cout)

Slide 3 Inheritance Chart iostream class ios istream iostream ostream

Slide 4 Unformatted Input Use stream extraction (>>) Input a character char c; cin >> c; Input a string char string[SIZE]; cin >> string;

Slide 5 Unformatted Input (Cont.) Example 1

Slide 6 Unformatted Input (Cont.) Use cin member function Input a character char c=cin.get(); Input a line char line[80]; cin.getline(line, 80); Input a string char string[SIZE]; cin.get(string, SIZE); or cin.read(string, SIZE);

Slide 7 Unformatted Input (Cont.) Other related cin member functions: 1. cin.eof(): returns 0 if not reach EOF. Returns 1 otherwise. 2. cin.ignore(n, c): ignore n characters until the character c 3. cin.putback(c): put the character c back to reading stream 4. cin.peek(): peek the next charcter in the reading stream 5. cin.gcount(): return # of characters last read

Slide 8 Unformatted Input (Cont.) Example 2 Example 3

Slide 9 Unformatted Output Use stream insertion (<<) Ouput a character char c; cout << c; Output a string char string[SIZE]; cout << string;

Slide 10 Unformatted Output (Cont.) Use stream insertion (<<) (Cont.) Ouput string address (by casting to type void *) char string[SIZE]; cout << “string address is “ string; Ouput variable address cout << “number address is “ << &number;

Slide 11 Unformatted Output (Cont.) Use cout member function Output a character char c=cout.put(); Output 2 characters char c1, c2 cout.put(c1).put(c2); Output a string char string[SIZE]; cout.write(string, SIZE); or cout.write(string, cin.gcount());

Slide 12 Unformatted Output (Cont.) Example 4

Slide 13 Formatted Input/Output Include stream manipulator: #include Can use either parametrized stream manipulator or member functions

Slide 14 Formatted Input ManipulatorMember function Explain setw(n)cin.width(n)use n characters to read

Slide 15 Formatted Input (Cont.) Example 5 Example 6

Slide 16 Formatted Output ManipulatorMember functionExplain setbase(n)/dec/oct /hex n=10(decimal),8( octal),16(hexadeci mal) setw(n)cout.width(n)n-1 characters to read setprecision(n)cout.precision(n)n places after decimal point setfill(c)cout.fill(c)Pad blank with character c

Slide 17 Formatted Output (Cont.) ManipulatorMember functionExplain setiosflags(flag)/s etf(flag),resetiosfl ags(flag)/unsetf(fl ag) cout.setf(flag), cout.unsetf(flag) Set, reset stream format state flags(flag)/flags()cout.flags(flag)/co ut.flag() Set format flag and return the previous format/show current format flag

Slide 18 ios Class Flags Format state flagExplain ios::skipwsSkip white space on input stream ios::left/ios::rightLeft/right justified ios::internalLeft justify sign, right justify number ios::showbaseSpecify base

Slide 19 ios Class Flags (Cont.) Format state flagExplain ios::dec/ios::oct/io s::hex Show values in base 10/8/16 ios::showpointShow decimal point ios::showposShow signs ios::scientificShow scientific notation

Slide 20 ios Class Flags (Cont.) Format state flagExplain ios::uppercaseShow uppercase for letters of output ios::fixedShow fixed floating point ios::showposShow signs ios::scientificShow scientific notation

Slide 21 Formatted Output (Cont.) Base Conversion

Slide 22 References Deitel & Deitel: C How to Program, 4th ed., Chapter 21, Prentice Hall