CS 103 Unit 14 - Streams

Similar documents
CS 103 Unit 14 - Streams

CSCI 104 Overview. Mark Redekopp David Kempe

CSCI 104. Rafael Ferreira da Silva. Slides adapted from: Mark Redekopp and David Kempe

CS103L SPRING 2018 UNIT 7: FILE I/O

STL. Prof Tejada Week 14

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

Lecture 5 Files and Streams

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

Introduction. Lecture 5 Files and Streams FILE * FILE *

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

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

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

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

Streams. Rupesh Nasre.

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

Chapter 3 - Notes Input/Output

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

Strings and Stream I/O

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

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

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

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

Input/Output Streams: Customizing

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

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

Chapte t r r 9

ENGI 1020 Introduction to Computer Programming R E Z A S H A H I D I J U L Y 2 6,

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

Setting Justification

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

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

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

10/23/02 21:20:33 IO_Examples

Lab: Supplying Inputs to Programs

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

Writing a Good Program. 7. Stream I/O

CS490: Problem Solving in Computer Science Lecture 3: Input/Output

Overview of Lecture. 1 Overloading I/O Operators. 2 Overloading << and >> for Fractions. 3 Formatted vs Unformatted Input

Chapter 2. Procedural Programming

A SHORT COURSE ON C++

Streams. Ali Malik

BITG 1113: Files and Stream LECTURE 10

G52CPP C++ Programming Lecture 17

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

C++ Input/Output: Streams

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

I/O streams

Chapter 11 Customizing I/O

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

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

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

Lecture 3 The character, string data Types Files

Stream States. Formatted I/O

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Ali Malik Handout 12 CS106L Winter 2018 Jan 30th, C++ Reference

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

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

CS11 Advanced C++ Lecture 2 Fall

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

Chapter 11 Customizing I/O

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Do not start the test until instructed to do so!

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

Chapter 3: Input/Output

Input and Output File (Files and Stream )

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

Introduction to C ++

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

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

Programming II with C++ (CSNB244) Lab 10. Topics: Files and Stream

CSCI 104 Exceptions. Mark Redekopp David Kempe

CS 1428 Review. CS 2308 :: Spring 2016 Molly O Neil

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

C++ files and streams. Lec 28-31

Chapter 14 Sequential Access Files

CS 103: Introduction to Programming Fall Written Final Exam 12/11/16, 4:30PM 6:30PM

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

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

Why Is Repetition Needed?

Starting to Program in C++ (Basics & I/O)

Chapter 11 Customizing I/O

CSC 306 LEXICAL ANALYSIS PROJECT SPRING 2017

What we will learn about this week:

Unit 1 : Principles of object oriented programming

C++ Input/Output Chapter 4 Topics

Week 5: Files and Streams

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

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

Advanced I/O Concepts

A <Basic> C++ Course

CS242 COMPUTER PROGRAMMING

Simple File I/O.

CSE 100: STREAM I/O, BITWISE OPERATIONS, BIT STREAM I/O

C++ Lab 02 - Command Line Arguments and Strings

A <Basic> C++ Course

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

Objects and streams and files CS427: Elements of Software Engineering

Lab # 02. Basic Elements of C++ _ Part1

Understanding main() function Input/Output Streams

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

Chapter 12. Streams and File I/O

Transcription:

CS 103 Unit 14 - Streams 1

2 I/O Streams '>>' operator reads from a stream (and convert to the desired type) Always skips leading whitespace ('\n', ' ', '\t') and stops at first trailing whitespace '<<' operator used to write data to an output stream 'endl' forces a flush Flush forces the OS to move data from the internal OS stream to the actual output device (like the monitor) input stream (user types all at once): #include<iostream> using namespace std; 6 1 7 5 \t y... int main(int argc, char *argv[]) { cout << "\tit was the" << endl; cout << 4; #include<iostream> using namespace std; int main(int argc, char *argv[]) { int dummy, x; cin >> dummy >> x; input stream: \t y... output stream in OS: \t I t w a s t h e \n 4 output stream after flush: 4 It was the

3 Kinds of Streams I/O streams Keyboard (cin) and monitor (cout) File streams Contents of file are the stream of data #include <fstream> and #include <iostream> ifstream and ofstream objects

4 When Does It Fail char buf[40]; ifstream inf(argv[1]); fp File text T h e e n d. \n EOF inf >> buf; fp File text T h e e n d. \n EOF For file & eventually stringstreams the stream doesn't fail until you read PAST the EOF inf >> buf; buf fp File text EOF BAD FAIL T h e \0 0 0 0 T h e e n d. \n EOF EOF BAD FAIL buf e n d. \0 0 0 0 inf >> buf; fp File text T h e e n d. \n EOF EOF BAD FAIL buf e n d. \0 1 0 1

5 Which Option? #include<iostream> #include<fstream> using namespace std; int main() { vector<int> nums; ifstream ifile("data.txt"); int x; while(!ifile.fail() ){ ifile >> x; nums.push_back(x);... Need to check for failure after you extract but before you store/use data.txt 7 8 EOF nums #include<iostream> #include<fstream> using namespace std; int main() { vector<int> nums; ifstream ifile("data.txt"); int x; while( 1 ){ ifile >> x; if(ifile.fail()) break; nums.push_back(x);... int x; while( ifile >> x ){ nums.push_back(x);... A stream returns itself after extraction A stream can be used as a bool (returns true if it hasn't failed)

6 Pattern for File I/O or Streams Step 1: Try to read data (>> or getline) Step 2: Check if you succeeded or failed Step 3: Only use the data read from step 1 if you succeeded If you read and then blindly use the data you will likely get a bogus data value at the end

7 Recall How To Get Lines of Text Using the >> operator to get an input string of text (char * or char [] variable passed to cin) implicitly stops at the first whitespace How can we get a whole line of text (including spaces) cin.getline(char *buf, int bufsize); ifile.getline(char *buf, int bufsize); Reads max of bufsize-1 characters (including newline) But getline() uses char* (C-Strings) what if we want to use C++ strings??? input.txt The fox jumped over the log. The bear ate some honey. The CS student solved a hard problem. #include <iostream> #include <fstream> using namespace std; int main () { char myline[100]; int i = 1; ifstream ifile ("input.txt"); if( ifile.fail() ){ // can't open? return 1; ifile.getline(myline, 100); while (! ifile.fail()) { cout << i++ << ": " << myline << endl; ifile.getline(myline, 100); ifile.close(); return 0; 1: The fox jumped over the log. 2: The bear ate some honey. 3: The CS student solved a hard problem.

8 C++ String getline() C++ string library (#include <string> defines a global function (not a member of ifstream or cin) that can read a line of text into a C++ string Prototype: istream& getline(istream &is, string &str, char delim); is = any input stream (ifstream, cin), etc.) str = A C++ string that it will fill in with text delim = A char to stop on (by default it is '\n') which is why its called getline Returns the updated istream (the 'is' object you passed in as the 1 st arg) The text from the input stream will be read up through the first occurrence of delim and placed into str. The delimiter will be stripped from the end of str and the input stream will be pointing at the first character after delim. int line_no = 0; ifstream myfile(argv[1]); string myline; while ( getline( myfile, myline ) ) { cout << "Read line: " << myline << endl;

STRINGSTREAMS 9

10 Introducing Stringstreams I/O streams Keyboard (cin) and monitor (cout) File streams Contents of file are the stream of data #include <fstream> and #include <iostream> ifstream and ofstream objects Stringstreams Contents of a string are the stream of data #include <sstream> and #include <iostream> sstream object

11 C++ String Stream If streams are just sequences of characters, aren't strings themselves like a stream? The <sstream> library lets you treat C++ string objects like they were streams Why would you want to treat a string as a stream? Buffer up output for later display Parse out the pieces of a string Data type conversions Very useful in conjunction with string's getline(...)

12 C++ String Stream Use << and >> to convert numbers into strings (i.e. 12345 => "12345") #include<sstream> using namespace std; int main() { stringstream ss; int num = 12345; ss << num; string strnum; ss >> strnum; num 12345 getp ss getp ss EOF 1 2 3 4 5 EOF return 0; strnum "12345" getp ss 1 2 3 4 5 EOF sstream_test1.cpp

13 C++ String Stream Use << and >> to convert strings into numbers (i.e. "12345" => 12345) #include<sstream> using namespace std; int main() { stringstream ss; string strnum = "12345"; ss << strnum; int num; ss >> num; return 0; strnum num "12345" 12345 getp ss getp ss getp ss EOF 1 2 3 4 5 EOF 1 2 3 4 5 EOF sstream_test2.cpp

14 C++ String Stream Can parse a string of many values into separate variables #include<sstream> using namespace std; int main() { stringstream ss; ss << "2.0 35 a" getp ss getp EOF double x, int y; char z; ss >> x >> y >> z; ss 2. 0 3 5 a EOF return 0; x y 2.0 35 getp ss 2. 0 3 5 a EOF sstream_test3.cpp z 'a'

15.str() str() member function will return a string with the contents of whatever is in the stream #include<sstream> using namespace std; int main() { stringstream ss; ss << 2.0 << " " << 35; ss << " " << 'a'; string s = ss.str(); getp ss getp ss EOF 2. 0 3 5 a EOF return 0; s "2.0 35 a" getp ss 2. 0 3 5 a EOF sstream_test4.cpp

16 C++ String Stream Beware of re-using the same stringstream object for multiple conversions. It can be weird. Make sure you clear it out between uses and re-init with an empty string Or just make a new stringstream each time stringstream ss; //do something with ss ss.clear(); ss.str(""); // now you can reuse ss // or just declare another stream stringstream ss2;

17 Exercise What's in each variable after execution? text num val string text; int num; double val; stringstream ss("hello 103 2.0"); ss >> text >> num >> val;

18 Exercises In class exercises Stringstream_in Stringstream_out Date

19 Keyboard (use ) Yes Choices Where is my data? File (use ) Do I know how many? String (use ) No

20 Choices Is it delimited? Yes Text What type of data? No Integers/ Doubles

21 Choosing an I/O Strategy Is my data delimited by particular characters? Yes, stop on newlines: Use getline() Yes, stop on other character: User getline() with optional 3 rd character No, Use >> to skip all whitespaces and convert to a different data type (int, double, etc.) If "yes" above, do I need to break data into smaller pieces (vs. just wanting one large string) Yes, create a stringstream and extract using >> No, just keep the string returned by getline() Is the number of items you need to read known as a constant or a variable read in earlier? Yes, Use a loop and extract (>>) values placing them in array or vector No, Loop while extraction doesn't fail placing them in vector Remember: getline() always gives text/string. To convert to other types it is easiest to use >>

22 In-Class Exercises Wordcount

23 getline() and stringstreams Imagine a file has a certain format where you know related data is on a single line of text but aren't sure how many data items will be on that line Can we use >>? No it doesn't differentiate between different whitespace (i.e. a ' ' and a '\n' look the same to >> and it will skip over them) We can use getline() to get the whole line, then a stringstream with >> to parse out the pieces int num_lines = 0; int total_words = 0; ifstream myfile(argv[1]); string myline; while( getline(myfile, myline) ){ stringstream ss(myline); string word; while( ss >> word ) { total_words++; num_lines++; double avg = (double) total_words / num_lines; cout << "Avg. words per line: "; cout << avg << endl; The fox jumped over the log. The bear ate some honey. The CS student solved a hard problem.

24 Using Delimeters Imagine a file has a certain format where you know related data is on a single line of text but aren't sure how many data items will be on that line Can we use >>? No it doesn't differentiate between different whitespace (i.e. a ' ' and a '\n' look the same to >> and it will skip over them) We can use getline() to get the whole line, then a stringstream with >> to parse out the pieces Text file: garbage stuff (words I care about) junk vector<string> mywords; ifstream myfile(argv[1]); string myline; getline(myfile, myline, '('); // gets "garbage stuff " // and throws away '(' getline(myfile, myline, ')' ); // gets "words I care about" // and throws away ')'` stringstream ss(myline); string word; while( ss >> word ) { mywords.push_back(word); 0 1 2 3 mywords "words" "I" "care" "about"