Upload
others
View
13
Download
0
Embed Size (px)
Citation preview
Chapter 7Streams and Text File I/O
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
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;
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
ostream and cout streams
The insertion operator supports various standard data types:
Int Char Double Bool Float String Cstring
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
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.
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
Output formatting
Output formatting
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);
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);
• 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
• 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
• 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
string firstName; string lastName; stringstream outSS; outSS << firstName << " "; outSS << lastName; string name = outSS.str();
ostringstreams
• 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
• 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
• 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
Stream Errors
example: error.cpp