Standard C++ Input/Output and String Classes

Slides:



Advertisements
Similar presentations
Chapter 10.
Advertisements

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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Standard.
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 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
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.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
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.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
FILE HANDLING IN C++.
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.
Chapter 9 Streams: Input stream: source of characters. Output stream: destination for characters. Up to now the input stream has defaulted to the keyboard.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
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
UNIT VI Streams & Files in C++: Stream classes, stream errors, disk file I/O with streams, File pointers, Error handling in file I/O. File I/O with members.
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. 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.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
Std Library of C++ Part 2. vector vector is a collection of objects of a single type vector is a collection of objects of a single type Each object in.
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.
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.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
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.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
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
Chapter 14: Sequential Access Files
© by Pearson Education, Inc. All Rights Reserved.
Introduction to C++ (Extensions to C)
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
From C-strings to C++ strings: Abstraction at Work
The Selection Structure
More about OOP and ADTs Classes
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
Chapter 2 part #3 C++ Input / Output
Array Lists Chapter 6 Section 6.1 to 6.3
Standard Input/Output Streams
Standard Input/Output Streams
Chapter 3 Introduction to Classes, Objects Methods and Strings
File I/O Streams, files, strings 1 1.
Lecture 5A File processing Richard Gesick.
Introduction to C++ Programming
More about OOP and ADTs Classes
files Dr. Bhargavi Goswami Department of Computer Science
Programming with Data Files
Chapter 3: Input/Output
Chapter 3 Input output.
Capitolo 1 – Introduction C++ Programming
Chapter 2 part #3 C++ Input / Output
Today’s Objectives 28-Jun-2006 Announcements
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
IO Overview CSCE 121 Strongly influenced by slides created by Bjarne Stroustrup and Jennifer Welch.
Presentation transcript:

Standard C++ Input/Output and String Classes Chapter 5 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Chapter Contents 5.1 The C++ Standard I/O Classes 5.2 The C++ String Types 5.3 Case Study: Text Editing 5.4 Introduction to Pattern Matching (optional) 5.5 Introduction to Data Encryption (optional) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Chapter Objectives Study in detail C++ classes for I/O Look at C++ hierarchy for I/O classes In particular for file I/O Study in detail the C++ string class Illustrate with use of string operators used in text editors (Optional) Introduce basic ideas of pattern matching (Optional) Introduce basic methods of data encryption Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

The C++ Standard I/O Classes Input viewed as a stream of characters Flowing from some source into an executing program Flowing from program to output device Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

The C++ Standard I/O Classes C++ has <iostream> library istream for input ostream for output Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

The istream Class Models flow of characters from input device to program Characters enter an istream object Object transmits characters from device to program Input operator >> Extraction operator istreamObject >> variable; Stream states accessed with functions .good(), .bad(), .fail(), .eof() Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

The istream Class Input manipulators Specify whether or not to skip white space noskipws or skipws Note Table 5-2 in text for Input Stream Operations and Methods Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

The ostream Class Models flow of characters from executing program to output device Characters enter ostream object Transmitted to specified output device Output operator << Insertion operator ostreamObject << expression; Note Table 5-3 in text for Output Stream Operations and Methods Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

The ostream Class Standard ostream objects cout for normal output cerr and clog for error and diagnostic messages Buffered streams (cout and clog) Held in the stream until it is flushed Contrast cerr which is sent to the output device immediately Can cause confusion when debugging Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

The ostream Class Format control Examples endl to send a newline Format manipulators used to specify various format features Examples endl to send a newline showpoint to specify that decimal points be used in output of reals Note Table 5-4 in text, Format Manipulators Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

File I/O: ifstream, ofstream Classes Stream object must be constructed for purpose of receiving/sending I/O Called opening a stream to the file Stream activities Declaring ifstream inStream; Opening ifStream.open(file_name); Closing ifStream.close(); Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

File I/O: ifstream, ofstream Classes File opening modes specify various properties of file being opened Examples input, output, append. Note Table 5-5 in text, File-Opening Modes Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

The I/O Class Hierarchy Characteristics of OOP hold Encapsulation Inheritance Polymorphism Note demo of file I/O, Fig 5-1 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

The C++ String Class Variety of constructors provided for defining strings Define an empty string String s; Define a string initialized with another string String s("some other string); Note further options in text, Table 5-7 String Constructors Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

The C++ String Class Storage differs by implementation May use char arrays May use dynamic storage Note Table 5-8 in text, String Storage Information Methods Input and output Use insertion << and extraction >> operators getline () for reading a string and including white spaces Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

The C++ String Class Editing operations provided such as appending inserting erasing replacing swapping Note table 5-10 in text, String Editing Operations Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

The C++ String Class Copiers – make copies of part or all of a string Operators = and += Function assign() Accessing Individual Characters Use overloaded subscript operator [ ] String element accessors Functions such as find(), find_first_of() See Table 5-12 in text Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

The C++ String Class Comparisons String conversions Overloaded operators for <, >, ==, etc. Also compare() function which returns a negative, 0, or positive value for <, ==, or > String conversions When C-style string needed instead of a string object Converts to an array of char Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

String Streams Possible to connect a stream to a string Read input from the string istringstream Write output to the string ostringstream stringstream (both input, output) Also known as in-memory I/O String stream library <sstream> Note Table 5-14 in text, String Stream Operations Demonstration program, Fig. 5-2 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Case Study: Text Editing Operations provided Construct text editor given names of input, output file Run editor – accept, carry out user commands Display menu of editing commands Insert string into current line of text Delete string from current line of text Replace string in current line of text with different text Get next line of text Wrap up editing process Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Case Study: Text Editing Source code provided to study OOP principles incorporated Data members Operations Declaration file, TextEditor.h, Fig. 5-3A Definition file, TextEditor.cpp, Fig 5-3B Driver program, driver.cpp, Fig 5-3C Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Intro to Pattern Matching Uses Text editing Searching file with Unix's grep One method … "brute force" Look for pattern starting at each successive character Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Pattern Matching A better way Instead of shifting to the right in text each time Shift until we know there will be at least a match in the first character of pattern Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Intro to Data Encryption Definition: Encryption is the coding of information to keep it secret Accomplished by transforming From a string of characters with information To a new string that is the coded message or the ciphertext The ciphertext may be safely transmitted At a later time the ciphertext is deciphered into plaintext Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Data Encryption Simplest encryption schemes use substitution Each letter replaced by some other letter according to a fixed rule Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Data Encryption Improved substitution method is to use a keyword Specifies several different displacements Vignère cipher Keyword added character by character to plane text string Each character represented by position in the string Addition carried out modulo 26 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Vignère Cipher Example Character set, positions given by Keyword is DAGGER Plane text IDESOFMARCH thus encrypted Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Substitution Table Table of substitutions given Resulting cipher text is Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Permutation Cipher Characters in plaintext or blocks rearranged by specified pattern Ciphertext thus produced Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Data Encryption Standard (DES) Developed by federal government and IBM corporation in 1970s Input is a 64 bit string representing a block of characters Output string also 64 bit string of cipher text for the block Encryption done with series of permutations and substitutions Was proven to be breakable Replaced in 1999 by Advanced Encryption Standard (AES) Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3

Public-Key Encryption Both sender and receiver must know key Must be transmitted in some secure manner Public-key encryption uses two keys One for encryption, exactly one corresponding key for decryption Many such pairs of keys exist, easily computed Nearly impossible to determine decryption key knowing only encryption key Encryption key made public, only receiver knows decryption key Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3