20
Chapter 7 Streams and Text File I/O

Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

Chapter 7Streams and Text File I/O

Page 2: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

Streams

C++ Input and output are based on streams - a sequence of bytes flowing in and out of a program.

C++ Program

Input Source

Output Sink

Input Stream

Output Stream

Page 3: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

ostream and cout streams

An ostream (output stream) is a class that supports output for programs.

Available with #include <iostream>

ostream provides the << operator - insertion operator

The insertions operator converts different types of data into a sequence of characters.

ostream cout;

Page 4: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

ostream and cout streams

The ostream buffer is used to read in data one character at a time and temporarily store until printed to the screen.

AGE10

0x2000x2010x2020x2030x204

int age = 10; cout << "AGE"; cout << age;

Console

Page 5: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

ostream and cout streams

The insertion operator supports various standard data types:

Int Char Double Bool Float String Cstring

Page 6: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

An istream (input stream) is a class that supports reading input for programs.

Available with #include <iostream>

istream provides the >> operator - extraction operator

The extraction operator to extract data from a buffer and feed it into different types of variables in the program.

istream cin;

istream and cin streams

Page 7: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

istream and cin streams

• cin is a predefined istream that is associated by default with the systems standard input (keyboard).

• >> operator

1. Skips whitespace

2. Extracts characters

3. Stops at next whitespace

4. Converts extracted characters to targets variable type

5. Stores result into variable.

Page 8: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

Output formatting

• Manipulators are used along with the insertion operator or extraction operator to adjust the way output appears.

• Available with #include <iomanip> or #include <ios>

• Affects all subsequent output because the manipulator is set on the buffer

double myFloat = 12.223; cout << setprecision(3) << myFloat << endl;

12.2

example: manipulators.cpp

Page 9: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

Output formatting

Page 10: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

Output formatting

Page 11: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

Output formatting

• endl - inserts a newline character into the buffer, then flushes the buffer (clears it).

• Flush - flushes the buffer without new line.

• Manipulators are actually functions that are called from cout.

setprecision(3); // does not affect output cout << setprecision(3);

Page 12: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

istringstreams

• String streams are a way for a programmer to read in strings as an input (rather than a keyboard) or output to a buffer rather than console.

• istringstream can be used with like cin and getline()

• Use istringstreams with #include <sstream>

string userInfo = "example"; istringstream inSS(userInfo);

Page 13: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

• Common use cases for string streams are processing input line by line

example: stringstream.cpp

int countWords(string str) { // breaking input into word using string stream stringstream s(str); // Used for breaking words string word; // to store individual words

int count = 0; while (s >> word) count++; return count; }

istringstreams

Page 14: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

• Common use cases for string streams are processing input line by line

string fullName; istringstream inSS;

inSS.str(fullName); // puts fullName in inSS buffer inSS.clear(); // clears the buffer

istringstreams

Page 15: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

• String streams can also be used as a substitute for an output stream

• ostringstream can be used with the insertion operator <<

• << inserts characters into ostringstream

• .str( ) - reads what is in ostringstream’s buffer and assigns to string.

ostringstreams

Page 16: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

string firstName; string lastName; stringstream outSS; outSS << firstName << " "; outSS << lastName; string name = outSS.str();

ostringstreams

Page 17: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

• Use the fstream library to read in files.

ifstream inputFile; Declares an input file stream named inputFile.

• .open(“filename”); Opens file so that program can begin streaming.

• .close(); Closes the file and stream.

File I/O

example: readFile.cpp

Page 18: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

• Use the fstream library to read in files.

ofstream outputFile; Declares an output file stream and file named outputFile.

Will create file if it doesn’t exist

• .open(“filename”); Opens file so that program can begin streaming.

• .close(); Closes the file and stream.

• use << (insertion operator) to output to file.

File I/O

example: outputFile.cpp

Page 19: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

• A Stream Error occurs when insertion or extraction fails, causing the stream to error.

• Common Input Stream errors:

• Reading value to big for data type

• Reading in string instead of number

Stream Errors

Page 20: Chapter 7egabrielsen/cs1342/Chapter7.pdf · 2019-09-27 · Chapter 7 Streams and Text File I/O. Streams C++ Input and output are based on streams - a sequence of bytes flowing in

Stream Errors

example: error.cpp