70 likes | 169 Views
EECS498-006 Lecture 18. Savitch N/A String Streams. Recall Stream Definition. A general definition of a stream A sequence of bytes, interpreted in groups of appropriate size, that can be used as input or output
E N D
EECS498-006 Lecture 18 Savitch N/A String Streams
Recall Stream Definition • A general definition of a stream • A sequence of bytes, interpreted in groups of appropriate size, that can be used as input or output • Input stream: Such a sequence of bytes, used as input, to set memory locations to appropriately typed values • Output stream: Such a sequence of bytes, used as output, to screen, data file, etc. • Currently known streams • Input: cin (keyboard), ifstream (input data file stream) • Output: cout (keyboard), ofstream (output data file stream) Andrew M Morgan
Another Type Of Stream • A general definition of a string • A sequence of characters • Characters are stored in one byte of data each, so really, this definition could also read "a sequence of bytes" • Same definition as a stream from previous page. • Strings can be used as input or output streams • istringstream: Data type that will use a string as an input stream • ostringstream: Data type that will use a string as an output stream • Both string stream types are in a header file named <sstream> • Standard stream operators (<< and >>) are available Andrew M Morgan
Output String Streams • Output string streams allow a user to "print" items of any data type to a sequence of characters (string) • Often this can be an easy way to form a printable collection of values all of which are different data types • ostringstream oss; //Declares an output string stream object • oss.str(): Returns the string currently stored in the output string stream object oss • operator<<: Used to add values to the sequence of characters stored in the string stream Andrew M Morgan
Output String Stream Example #include <sstream> #include <string> #include <iostream> #include <algorithm> using namespace std; class RectangleClass { private: double length; double width; public: RectangleClass( double inLen, double inWid ):length(inLen), width(inWid) { ; } double getArea( ) const { return (length * width); } friend ostream& operator<<(ostream &os, const RectangleClass &rect); }; ostream& operator<<( ostream &os, const RectangleClass &rect ) { os << "Len: " << rect.length << " Wid: " << rect.width; return (os); } int main() { RectangleClass r1(5.5, 4.11); ostringstream oss; string strObj; oss << "Rectangle Information" << endl; oss << r1 << endl; oss << "Area: " << r1.getArea() << " As Int: " << static_cast< int >(r1.getArea()) << endl; strObj = oss.str(); cout << "Contents of strObj:" << endl; cout << strObj; cout << "End Of strObj contents" << endl; cout << "Contents of oss.str():" << endl; cout << oss.str(); cout << "End of oss.str() contents" << endl; cout << "Loc of first digit: " << oss.str().find_first_of("0123456789") << endl; return (0); } Contents of strObj: Rectangle Information Len: 5.5 Wid: 4.11 Area: 22.605 As Int: 22 End Of strObj contents Contents of oss.str(): Rectangle Information Len: 5.5 Wid: 4.11 Area: 22.605 As Int: 22 End of oss.str() contents Loc of first digit: 27 Andrew M Morgan
Input String Streams • Input string streams allow a user to read in values of different data types easily from a sequence of characters • Often used by reading in a full line from a text file, then parsing it element-by-element • iss.str(string inStr): Sets the sequence of characters to use as an input stream to the contents of the inStr parameter • operator>>: Consumes characters from the input string stream to be used as values • Usual state-checking functions available (fail, etc) Andrew M Morgan
Input String Stream Example #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <string> using namespace std; int main() { int i; const string filename = "fileIn.txt"; ifstream inFile; string lineString; istringstream iss; int tempInt; string personName; int personId; vector< int > intVals; int numLinesInFile; numLinesInFile = 0; inFile.open(filename.c_str()); if (!inFile) { cout << "ERROR: Unable to open:" << filename << endl; exit(2); } getline(inFile, lineString); while (inFile) { inFile >> personName >> personId; if (inFile) { numLinesInFile++; getline(inFile, lineString); iss.clear(); iss.str(lineString + " "); while (iss) { iss >> tempInt; if (iss) { intVals.push_back(tempInt); } } cout << "Name: " << personName << " Id: " << personId << " #Values: " << intVals.size() << " Values: "; for (i = 0; i < intVals.size(); i++) { cout << intVals[i] << " "; } cout << endl; intVals.clear(); } } cout << endl << "Total number of people read in: " << numLinesInFile << endl; return (0); } Input File Name ID V1 V2 V3 V4 V5 V6 This is a hdr line Andrew 1000 98 10 77 107 Steve 2000 3 9 983 61 38 119 Jeff 500 Bob 1600 87 4 Joe 3500 41 8 61 62 81 Output Name: Andrew Id: 1000 #Values: 4 Values: 98 10 77 107 Name: Steve Id: 2000 #Values: 6 Values: 3 9 983 61 38 119 Name: Jeff Id: 500 #Values: 0 Values: Name: Bob Id: 1600 #Values: 2 Values: 87 4 Name: Joe Id: 3500 #Values: 5 Values: 41 8 61 62 81 Total number of people read in: 5 Andrew M Morgan