Lecture 3 The character, string data Types Files

Similar documents
Chapter 3 - Notes Input/Output

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

I/O Streams and Standard I/O Devices (cont d.)

Input and Output File (Files and Stream )

Week 5: Files and Streams

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

BITG 1113: Files and Stream LECTURE 10

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

what are strings today: strings strings: output strings: declaring and initializing what are strings and why to use them reading: textbook chapter 8

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

6. User Defined Functions I

Definition Matching (10 Points)

Formatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input

Streams in C++ Stream concept. Reference information. Stream type declarations

Chapte t r r 9

Engineering Problem Solving with C++, Etter

Objectives. In this chapter, you will:

Objects and streams and files CS427: Elements of Software Engineering

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

C++ Programming Lecture 10 File Processing

C++ Input/Output Chapter 4 Topics

Chapter 3: Input/Output

Why Is Repetition Needed?

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

Lecture 5 Files and Streams

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Chapter 4 - Notes Control Structures I (Selection)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

C++ Programming: From Problem Analysis to Program. Design, Fifth Edition. Chapter 1: An Overview of Computers and Programming Languages

Introduction. Lecture 5 Files and Streams FILE * FILE *

C++ Input/Output: Streams

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Object Oriented Programming Using C++ UNIT-3 I/O Streams

CS2141 Software Development using C/C++ Stream I/O

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

UEE1303(1070) S 12 Object-Oriented Programming in C++

by Pearson Education, Inc. All Rights Reserved. 2

COMP322 - Introduction to C++

In this chapter, you will learn about: An Array Type for Strings. The Standard string Class. Vectors. Introduction Computer Science 1 CS 23021

std::string Quick Reference Card Last Revised: August 18, 2013 Copyright 2013 by Peter Chapin

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Understanding main() function Input/Output Streams

Copyright 2003 Pearson Education, Inc. Slide 1

COMP322 - Introduction to C++

Week 3: File I/O and Formatting 3.7 Formatting Output

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Topic 2. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Fundamental File Processing Operations 2. Fundamental File Processing Operations

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

CSCE 206: Structured Programming in C++

Chapter 8 - Notes User-Defined Simple Data Types, Namespaces, and the string Type

Simple File I/O.

CS101 Linux Shell Handout

Unit-V File operations

LECTURE 02 INTRODUCTION TO C++

Software Design & Programming I

File I/O and String Manipula3ons CS 16: Solving Problems with Computers I Lecture #11

120++: a C++ Subset Corresponding to A Project-Based Introduction to C++

Working with Strings. Lecture 2. Hartmut Kaiser. hkaiser/spring_2015/csc1254.html

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Fundamentals of Programming CS-110. Lecture 2

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

4 Strings and Streams. Testing.

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

Chapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Chapter-12 DATA FILE HANDLING

Multiple Choice Questions (20 questions * 6 points per question = 120 points)

More on File I/O Strings in C++ CS 16: Solving Problems with Computers I Lecture #10

COMP322 - Introduction to C++

Chapter void Test( int, int, int ); // Function prototype int main() // Function heading { int h; // Local variable

Streams - Object input and output in C++

Introduction to Programming EC-105. Lecture 2

VuZs Team's Work. CS201 Spring Solved by vuzs Team with Reference Written by Administrator Wednesday, 19 May :52

PROGRAMMING EXAMPLE: Checking Account Balance

Computer Programming : C++

We will exclusively use streams for input and output of data. Intro Programming in C++

Name Section: M/W T/TH Number Definition Matching (8 Points)

Dodatak D Standardna string klasa

Chapter 5: Loops and Files

File I/O Christian Schumacher, Info1 D-MAVT 2013

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

Summary of basic C++-commands

Module C++ I/O System Basics

Chapter 9 : I/O Streams and Data Files (pp )

Basics of C++ C++ Overview Elements I / O Assignment and Comments Formatted Output. Dr.Hamed Bdour

Transcription:

Lecture 3 The character, string data Types Files

The smallest integral data type Used for single characters: letters, digits, and special symbols Each character is enclosed in single quotes 'A', 'a', '0', '*', '+', '$', '&' A blank space is a character Written ' ', with a space left between the single quotes 2 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

Different character data sets exist ASCII: American Standard Code for Information Interchange Each of 128 values in ASCII code set represents a different character Characters have a predefined ordering based on the ASCII numeric value 3 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

4 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

5 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

6 C++ Programming: From Problem Analysis to Program Design, Seventh Edition

To use the data type string, the program must include the header file <string> The statement: string name = "William Jacob"; declares name to be a string variable and also initializes name to "William Jacob" The first character, 'W', in name is in position 0, the second character, 'i', is in position 1, and so on C++ Programming: Program Design Including Data Structures, Second Edition 8

The variable name is capable of storing any size string Binary operator + (to allow the string concatenation operation), and the array subscript operator [], have been defined for the data type string For example, If str1 = "Sunny", the statement stores the string "Sunny Day" into str2: str2 = str1 + " Day"; C++ Programming: Program Design Including Data Structures, Second Edition 9

Suppose we have the following declarations: string str1 = "Hello"; string str2 = "Hi"; string str3 = "Air"; string str4 = "Bill"; C++ Programming: Program Design Including Data Structures, Second Edition 10

Length returns the number of characters currently in the string The syntax to call the length function is: strvar.length() where strvar is variable of the type string length has no arguments length returns an unsigned integer The value returned can be stored in an integer variable C++ Programming: Program Design Including Data Structures, Second Edition 11

find searches a string for the first occurrence of a particular substring Returns an unsigned integer value of type string::size_type giving the result of the search The syntax to call the function find is: strvar.find(strexp) where strvar is a string variable and strexp is a string expression evaluating to a string The string expression, strexp, can also be a character C++ Programming: Program Design Including Data Structures, Second Edition 12

If successful, find returns the position in strvar where the match begins For the search to be successful the match must be exact If unsuccessful, find returns the special value string::npos ( not a position within the string ) To find next, strvar.find(strexp, Pos), where the Pos is the position to start searching with. C++ Programming: Program Design Including Data Structures, Second Edition 13

Append returns the string after concatenating with other string The syntax to call the length function is: strvar.append(strvar1) where strvar, strvar1 are variables of type string Append can also be used to concatenate substring of other string in a certain position The syntax to call the length function is: strvar.append(strvar1, 5, 7) where Append strvar1 from the fifth character and append 7 characters C++ Programming: Program Design Including Data Structures, Second Edition 14

substr returns a particular substring of a string The syntax to call the function substr is: strvar.substr(expr1,expr2) The expression expr1 specifies a position within the string (starting position of the substring) The expression expr2 specifies the length of the substring to be returned C++ Programming: Program Design Including Data Structures, Second Edition 15

Replaces part of the string with another string string& replace (size_t pos, size_t len, const string& str); St1.replace(5, 9, St2); put 9 characters from St2 on the 5 th position from St1. C++ Programming: Program Design Including Data Structures, Second Edition 16

comapre returns an int represent the comparison between two strings The syntax to call the function substr is: int comp=strvar.compare(strvar1) Where comp is zero if the two strings are equal + if the strvar is bigger than strvar1, else is negative The value of comp depends on the distance between the two strings C++ Programming: Program Design Including Data Structures, Second Edition 17

swap interchanges the contents of two string variables The syntax to use the function swap is strvar1.swap(strvar2); where strvar1 and strvar2 are string variables Suppose you have the following statements: string str1 = "Warm"; string str2 = "Cold"; After str1.swap(str2); executes, the value of str1 is "Cold" and the value of str2 is "Warm" C++ Programming: Program Design Including Data Structures, Second Edition 18

An input stream variable (cin) and extraction operator >> can read a string into a variable of the data type string Extraction operator Skips any leading whitespace characters and reading stops at a whitespace character Should not be used to read strings with blanks The function getline Reads until end of the current line Should be used to read strings with blanks C++ Programming: From Problem Analysis to Program Design, Third Edition 19

File: area in secondary storage to hold info File I/O 1. Include fstream header 2. Declare file stream variables 3. Associate the file stream variables with the input/output sources 4. Use the file stream variables with >>, <<, or other input/output functions 5. Close the files File Input/Output C++ Programming: From Problem Analysis to Program Design, Third Edition 20

The type is a bitmask type that describes an object that can store the opening mode for several iostreams objects. The distinct flag values (elements) are: C++ Programming: From Problem Analysis to Program Design, Third Edition 21

Use an EOF (End Of File)-controlled while loop The logical value returned by cin can determine if the program has ended input The syntax is: cin >> variable; while (cin) {. cin >> variable;. } C++ Programming: Program Design Including Data Structures, Second Edition 22

The function eof can determine the end of file status Like other I/O functions (get, ignore, peek), eof is a member of data type istream The syntax for the function eof is: istreamvar.eof() where istreamvar is an input stream variable, such as cin C++ Programming: Program Design Including Data Structures, Second Edition 23

File: open, seekg, eof, close, C++ Programming: From Problem Analysis to Program Design, Third Edition 24

#include <fstream> using namespace std; int main() { ifstream infile; ofstream outfile; double exam1, exam2, exam3, average; } infile.open( e:\\grades.dat ); outfile.open( e:\\average.dat ); infile >> exam1 >> exam2 >> exam3; average = (exam1 + exam2 + exam3) / 3.0; outfile << Average = << average << endl; infile.close(); outfile.close(); return 0; C++ Programming: From Problem Analysis to Program Design, Third Edition 25

You can let the user specify the name of the input and/or output file at execution time: C++ Programming: From Problem Analysis to Program Design, Fourth Edition 26