Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

Strings.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Streams and File I/O. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  I/O Streams  File I/O  Character.
File streams Chapter , ,
C/C++ Basics (VII) Streams and File I/O Berlin Chen 2003 Textbook: 1. Walter Savitch, “Absolute C++,” Addison Wesley, 2002 開發代理 2. Walter Savitch, “Problem.
Copyright © 2002 Pearson Education, Inc. Slide 1.
CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.
Chapter 3: Input/Output
CS-2303 System Programming Concepts
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 27P. 1Winter Quarter I/O Manipulation Lecture.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 12 Streams and File I/O Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 12 Streams and File I/O. Learning Objectives I/O Streams – File I/O – Character I/O Tools for Stream I/O – File names as input – Formatting output,
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
COP 3530 Data Structures & Algorithms Discussion Session 3.
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.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
4. Input/Output Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 C++ Input/Output: Streams The.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
Chapter 3: Input/Output
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
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.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Program Input and the Software Design Process ROBERT REAVES.
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.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
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.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Enumerated Types and Strings Types.
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.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
Chapter 9 Strings Copyright © 2016 Pearson, Inc. All rights reserved.
CMPE Data Structures and Algorithms in C++ September 7 Class Meeting
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Basic Input and Output Operations
COMP 2710 Software Construction File I/O
Copyright © 2003 Pearson Education, Inc.
Input and Output Chapter 3.
Standard Input/Output Streams
Standard Input/Output Streams
C Stuff CS 2308.
Learning Objectives String Class.
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.
Today’s Lecture I/O Streams Tools for File I/O
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included for string manipulation? how is.
Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
String What it is Why it’s useful
Programming Assignment #1 12-Month Calendar—
Chapter 3 Input output.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Programming Assignment #5
Differences between Java and C
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
Chapter 3: Expressions and Interactivity
Engineering Problem Solving with C++, Etter
CS150 Introduction to Computer Science 1
CS 144 Advanced C++ Programming February 7 Class Meeting
CS 144 Advanced C++ Programming February 5 Class Meeting
Reading from and Writing to Files
C++ Programming Lecture 20 Strings
Reading from and Writing to Files
Presentation transcript:

Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd 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, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Strings and Streams

Reading Assignment — Strings Chapter 9 of Absolute C++ CS-2303, A-Term 2012 Strings and Streams

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 CS-2303, A-Term 2012 Strings and Streams

C-Strings #include <cstring> Let Then 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' CS-2303, A-Term 2012 Strings and Streams

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 CS-2303, A-Term 2012 Strings and Streams

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

C-String Summary Familiar strings from C Same capabilities as strings in C Same hazards Same awkwardness CS-2303, A-Term 2012 Strings and Streams

Questions? CS-2303, A-Term 2012 Strings and Streams

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; // concatenation, assignment if (s1 <= s2) ... // comparison s4 = "Hello Mom!“ // automatic conversion from C-string Allocates memory long enough to hold both s1 and s2. CS-2303, A-Term 2012 Strings and Streams

CS-2303, A-Term 2012 Strings and Streams

Chapter 9 of Absolute C++ CS-2303, A-Term 2012 Strings and Streams

Questions? CS-2303, A-Term 2012 Strings and Streams

Streams Flow of characters Input stream Output stream Reading Assignment:– Absolute C++, Ch. 12 Flow of characters Input stream Flow into program From any source Keyboard, file, pipe, etc. Output stream Flow from program To any destination Screen, file, pipe, etc. CS-2303, A-Term 2012 Strings and Streams

Stream Usage Familiar streams Other 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 CS-2303, A-Term 2012 Strings and Streams

Stream Usage (continued) Class ifstream ofstream #include <iostream> #include <fstream> Part of std namespace using std::ifstream; using std::ofstream; CS-2303, A-Term 2012 Strings and Streams

Declaring Streams Like any other class variable:– Opening files 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 CS-2303, A-Term 2012 Strings and Streams

Declaring Streams Like any other class variable:– Opening files 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) CS-2303, A-Term 2012 Strings and Streams

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

Stream Manipulators setfill(), setw() Examples 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

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

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

Questions? CS-2303, A-Term 2012 Strings and Streams