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

Similar documents
The C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.

Chapter 12. Streams and File I/O

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

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

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

Chapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved

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

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

Engineering Problem Solving with C++, Etter

Chapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.

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

Software Design & Programming I

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

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Programming in C/C

Simple File I/O.

C++ Input/Output: Streams

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

Copyright 2003 Pearson Education, Inc. Slide 1

File Input / Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #9

What we will learn about this week:

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

Linked Lists in C and C++

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

Chapter 3 - Notes Input/Output

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Ch 6-8. C-Strings. April 30, Prof. Young-Tak Kim

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Your first C and C++ programs

Input/Output Streams: Customizing

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

COMP322 - Introduction to C++

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

Streams. Rupesh Nasre.

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Objects and streams and files CS427: Elements of Software Engineering

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

CS 103 Unit 14 - Streams

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

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

C++ for Python Programmers

Streams. Ali Malik

File I/O CS 16: Solving Problems with Computers I Lecture #9

BITG 1113: Array (Part 2) LECTURE 9

Fundamentals of Programming CS-110. Lecture 2

Lecture 5 Files and Streams

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

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Chapter 14 Sequential Access Files

Setting Justification

CS2141 Software Development using C/C++ C++ Basics

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

C++ Input/Output Chapter 4 Topics

Chapter 9. Strings. 國立雲林科技大學資訊工程研究所張傳育 (Chuan Yu Chang ) 博士

Introduction to Programming EC-105. Lecture 2

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

Programming in C/C

Streams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing

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

Computer Programming

Chapter 11 Customizing I/O

CS3157: Advanced Programming. Outline

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

COMP322 - Introduction to C++

Introduction. Lecture 5 Files and Streams FILE * FILE *

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Strings and Stream I/O

Come and join us at WebLyceum

CS 103 Unit 14 - Streams

Streams - Object input and output in C++

File Input/Output Streams in C++ CS 16: Solving Problems with Computers I Lecture #10

A Deeper Look at Classes

Getting started with C++ (Part 2)

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 7. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

LECTURE 15. String I/O and cstring library

Writing a Good Program. 7. Stream I/O

by Pearson Education, Inc. All Rights Reserved. 2

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

Input and Output File (Files and Stream )

CSI33 Data Structures

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

Streams and Basic File I/O Tools for Stream I/O Character I/O Inheritance

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

Introduction to C++ (Extensions to C)

Chapter 15 - C++ As A "Better C"

CS103L SPRING 2018 UNIT 7: FILE I/O

Name. CPTR246 Spring '17 (100 total points) Exam 2

Containers and the Standard Template Library (STL)

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

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

Chapter 10 Characters, Strings, and the string class

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

Chapter 11 Customizing I/O

C, C++, Fortran: Basics

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

C++ does not, as a part of the language, define how data are sent out and read into the program

Definition Matching (10 Points)

COP 3014: Fall Final Study Guide. December 5, You will have an opportunity to earn 15 extra credit points.

Transcription:

Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel) 1

Reading Assignment Strings Chapter 9 of Absolute C++ 2

Two kinds of strings in C++ C-String Not part of Standard Template Library Array of char terminated by null char '\0 Like strings in C String class Part of Standard Template Library (STL) Container for supporting w_char, other character sets Powerful tools for manipulating strings in natural way 3

C-Strings #include <cstring> Let char u[] = "This is another string!"; Then u[0] == 'T' u[1] == 'h' u[2] == 'i' u[20] == n' u[21] == 'g' u[22] == '!' u[23] == '\0' 4

C-Strings (continued) Usual string functions strcpy() strlen() strcmp() strcat() Command line args argv is array of c-strings argv[argc] is null pointer C++ input-output cout << "Wow!" << endl; char *c = "This is a string."; char d[25]; cout << c << endl; cin >> d; But watch out for size of array d = c; //assigns pointers, not characters 5

C-Strings (continued) getline(char *s, int length) Member function of cin Reads input up to new-line character Limited by length argument #include <cstring>... char a[80]; cin.getline(a, 80); cout << a << "End of Input" << endl; Also cin.get() cout.put() Many other character utilities 6

C-String Summary Familiar strings from C Same capabilities as strings in C Same hazards Same awkwardness 7

Questions? 8

Standard class string Part of Standard Template Library (STL) #include <string> Included in namespace std String variables and expressions Treated much like simple types Assign, compare, etc string s1, s3, s3, s4; s3 = s1 + s2; if (s1 <= s2)... s4 = "Hello Mom! Allocates memory long enough to hold both s1 and s2. // concatenation, assignment // comparison // automatic conversion from C-string CS-2303, A-Term 2012 Strings and Streams 1-9

10

Chapter 9 of Absolute C++ 11

Questions? 12

Streams Flow of characters Reading Assignment: Absolute C++, Ch. 12 Input stream Flow into program From any source Keyboard, file, pipe, etc. Output stream Flow from program To any destination Screen, file, pipe, etc. 13

Stream Usage Familiar streams cin Input stream object connected to standard input cout Output stream object connected to standard output Other streams To or from files Usage similar to cin, cout 12-14

Stream Usage (continued) Class ifstream ofstream #include <iostream> #include <fstream> Part of std namespace using std::ifstream; using std::ofstream; 15

Declaring Streams Like any other class variable: ifstream instream; ofstream outstream; Opening files instream.open("myinput.txt"); outstream.open("myoutput.txt); Absolute or relative pathname of file cin is an object of class ifstream cout is an object of class ofstream 12-16

Declaring Streams Like any other class variable: ifstream instream; ofstream outstream; Opening files instream.open("myinput.txt"); outstream.open("myoutput.txt", ios::app); Absolute or relative pathname of file Optional argument specifying append to file (if it exists) 12-17

Input #include <ifstream> ifstream filein("myfile.txt"); string word; filein >> word; // skips leading whitespace // stops at next whitespace CS-2303, A-Term 2012 Strings and Streams 18

Stream Manipulators setfill(), setw() Sets fill character and field width of stream Examples cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second << endl; cout.setw(2); // sets all field widths CS-2303, A-Term 2010 More about C++ Classes 19

More Stream Manipulators setprecision() Sets precisions (in decimal places) of floating point numbers eof() Returns a bool indicating whether the end of the stream has been reached True only after program attempts to read past last character of the stream Usage: cin.eof() CS-2303, A-Term 2010 More about C++ Classes 20

Other Useful Stream Functions getline() Reads stream to next newline, returns string get() Reads one character from input stream, returns int put() Writes one character to output stream A long list of manipulators at http://www.cplusplus.com/reference/iostream/ Absolute C++, 12.2 Stroustrup, 21.4.6 CS-2303, A-Term 2010 More about C++ Classes 21

Questions? 22