Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.

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

LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
CS-1030 Dr. Mark L. Hornick 1 IOStreams revisited Streams, strings, and files.
1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
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.
C++ plus. 2 Goals Some general C++ tips 3 C++ Tips is header file for a library that defines three stream objects Keyboard an istream object named cin.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
1 File I/O 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 3: Input/Output
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.
CSCE 121: Introduction to Program Design and Concepts J. Michael Moore Fall 2014 Set 11: More Input and Output 1 Based on slides created by Bjarne.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
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.
1 Chapter 4 Program Input and the Software Design Process.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Chapter 8 Data File Basics.
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 CS161 Introduction to Computer Science Topic #13.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
Streams, Files, and Formatting Chapter Standard Input/Output Streams t Stream is a sequence of characters t Working with cin and cout t Streams.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
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.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen 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.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
Program Input and the Software Design Process ROBERT REAVES.
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.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
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.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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.
CS212: Object Oriented Analysis and Design
Basic Input and Output Operations
Data Streams.
File I/O.
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.
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
when need to keep permanently
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Chapter 3: Input/Output
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Chapter 4 INPUT AND OUTPUT OBJECTS
File I/O.
Dale/Weems/Headington Program Input and the Software Design Process
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
Input/Output Streams, Part 1
File I/O in C++ I.
getline() function with companion ignore()
Presentation transcript:

Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen are the standard I/O devices. Later, when we need to add file I/O, we will see that files are also streams. Everything we just learned will be the same for files as it was for standard I/O.

C++ Stream I/O  C++ introduces the concept of a stream as the input/output object. (Java also uses the stream concept)  A stream contains characters coming from some input device  Or a stream contains characters going to some output device

C++ Stream I/O (cont) To use standard stream I/O, we include the header file iostream.h in our program To use file I/O, we include the header file fstream.h in our program. #include To use a character array as a stream, we include the header file strstrea.h in our program.

Input and Output (Standard) Input uses an istream Output uses an ostream cin is associated with the standard input device, i.e. the keyboard cout is associated with the standard output device, i.e. the monitor screen

Input and Output ( files ) Input uses an ifstream Output uses an ofstream inData is then associated with the input file outData is associated with the the output file #include ifstream inData; ofstream outData;

Extraction and Insertion To get characters from the input stream, we use the extraction operator >>. To put characters on the output stream, we use the insertion operator <<. The direction the operator points, makes it easy to remember which is which.

Extraction and Insertion (cont) cin >> someInt means extract (get) some integer from the input stream. cout << someInt means insert (put) some integer on the output stream

Streams Streams convert characters into values of appropriate data types. cin >> someInt looks for a sequence of characters which represents an integer. cout << "The answer is " << someInt first outputs a string and then outputs the characters representing the value of someInt.

Streams (cont) cin and cout operate on character, or text streams. Text streams have the concept of lines and '\n' is the newline character. cout << someInt << '\n';

Streams (cont) endl is a better way to represent the newline character since it is more in keeping with the concepts of abstraction. cout << someInt << endl;

get and getline Sometimes we want more control over the input or output stream. cin.get(ch) will get a single character. cin.getline(str,81) will get an entire line (up to 80 characters) and will consume the newline character. cin.ignore(80, '\n') skips 80 characters or skip to the start of the next line.

put and putback cout.put(ch) outputs a single character. cin.putback(ch) returns a character to the input stream.

Testing the state of a stream When processsing characters from the input stream, we often need to know when the stream is empty of characters. The stream returns a value for the success or failure of the last operation. cin >> someInt; // the priming read while (cin) {// the test for success process(someInt); // do loop if true cin >> someInt; // the looping read }