Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard.

Slides:



Advertisements
Similar presentations
© 2000 Scott S Albert Structured Programming 256 Chapter 7 Streams and File I/O.
Advertisements

1 Lecture 31 Handling Selected Topics Again!! File I/O Special Lectures.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Standard.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
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
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.
Program Input and the Software Design Process ROBERT REAVES.
UNFORMATTED INPUT OUTPUT. Topics to be discussed……………….. overloaded operators >> and and
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.
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
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 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
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.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
CHAPTER 7 DATA INPUT OUTPUT Prepared by: Lec. Ghader R. Kurdi.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
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.
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.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
Advanced Input and Output Object-Oriented Programming Using C++ Second Edition 10.
Chapter 3: Input/Output
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
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.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 13: Inheritance and Composition.
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.
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.
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.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
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.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
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?
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.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
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
Standard C++ Input/Output and String Classes
Chapter 14: Sequential Access Files
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CISC/CMPE320 - Prof. McLeod
Input and Output Chapter 3.
Standard Input/Output Streams
Lecture 5A File processing Richard Gesick.
Today’s Lecture I/O Streams Tools for File I/O
Chapter 3: Input/Output
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Managing console i/o operations
Chapter 3 Input output.
Chapter 4 INPUT AND OUTPUT OBJECTS
File I/O in C++ I.
File I/O.
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
File I/O in C++ I.
Presentation transcript:

Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard (cin) and the output stream has defaulted to the console window (cout).

Input stream can correspond to a text file. Sample lines of code: ifstream indata indata.open(“file name”) or indata.open(c-style string containing a file name)

indata >> variable reads up to white space This could be a tab character, a blank space, or ‘\n’ the end-of-line character If the variable is numeric, it will read up to the first non-digit The character it reads up to is still in the stream!! Useful for reading numerics or multiple strings on a line if they’re delimited by spaces.

getline(indata, s) s is a string object and reads up to ‘\n’ and discards the ‘\n’ Useful for reading strings that contain spaces. Typical when the string occupies one whole line of the stream

See demo programs. If you follow the instructions I gave at the beginning of the semester for creating a project, the data files are in the project folder. See example program from p. 378.

getting and ungetting: ifstream indata indata.get(char variable) will read a character indata.unget(char variable) will put it back See example on page 377 Not common

Stream Manipulators Allows formatting of displays. Some of the demo programs used this. See page 383 for specific manipulators.

ifstream is a stream for an input file ofstream is a stream for an output file fstream is for reading and writing to a file. Each of these is an istream or an ostream because of inheritance. Note the class hierarchy on streams (p. 380).

Advantage of streams and the inheritance hierarchy Can use the same code for different input sources or output destinations. Run the program in notes (from page 381) and test the following cases: A file containing data. A nonexistent file. An empty file. With data entered from the keyboard.

The istream class is a base class for other derived classes (inheritance) cin is an istream class. So is an object declared as type ifstream. This is what allows the read_data method from this sample program to be invoked with either cin or infile. Similar statements exist for ostream and ofstream classes.

Note the use of istream and ostream classes in the method signatures of demos 3 and 4. This allows input and output to come from (or go to) unspecified sources and destinations. Can write classes independent of data sources and output destinations.

Much better reuse. DO NOT have to rewrite stuff just because data sources change. This means you can also replace a console front end with a gui front end. But need string stream classes first.

String Streams: istringstream and ostringstream allows you to “read from” and “write to” a string. Though this might seem a bit strange it is incredibly valuable. Note the examples on p Note also the readtime.cpp program on p. 386.

For example an object that writes its own data can be given cout, an ofstream object, or an ostringstream object which can be put into a gui text box. This IS USEFUL for reuse and allows independence of the front end the user interacts with. Similar statements can be made for input.

demo05 shows how the Bank class from demo04 or demo06 can also be used with a gui interface as opposed to a console based one. Works because istringstream isa istream and ostringstream isa ostream. You will have to copy the bank class and header file from demo04 or demo06 to this project file along with the test harness.

Command line arguments (p. 389) All arguments are strings. In VC++.NET do the following to define the parameters: Project -> projectname properties. In the left pane, select debugging. Look for the command line Arguments entry and enter your data values. Example: encryption program of p

Note the random fact on p. 392 covering RSA and PGP.

Random Access files (section 9.6) You can skip this section