chapter 7egabrielsen/cs1342/chapter7.pdf · 2019-09-27 · chapter 7 streams and text file i/o....
TRANSCRIPT
![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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/1.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/2.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/3.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/4.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/5.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/6.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/7.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/8.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/9.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/10.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/11.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/12.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/13.jpg)
• 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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/14.jpg)
• 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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/15.jpg)
• 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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/16.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/17.jpg)
• 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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/18.jpg)
• 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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/19.jpg)
• 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](https://reader034.vdocument.in/reader034/viewer/2022042620/5f4be703ec00e311773c96d4/html5/thumbnails/20.jpg)
Stream Errors
example: error.cpp