In addition to standard stream I/O and file stream I/O, C++ stream I/O includes capabilities for inputting from, and outputting to, strings in memory. These capabilities often are referred to as in-memory I/O or string stream processing.
Input from a string is supported by class istringstream. Output to a string is supported by class ostringstream. The class names istringstream and ostringstream are actually aliases defined by the typedefs
typedef basic_istringstream< char > istringstream; typedef basic_ostringstream< char > ostringstream;
Class templates basic_istringstream and basic_ostringstream provide the same functionality as classes istream and ostream plus other member functions specific to in-memory formatting. Programs that use in-memory formatting must include the and header files.
One application of these techniques is data validation. A program can read an entire line at a time from the input stream into a string. Next, a validation routine can scrutinize the contents of the string and correct (or repair) the data, if necessary. Then the program can proceed to input from the string, knowing that the input data is in the proper format.
Outputting to a string is a nice way to take advantage of the powerful output formatting capabilities of C++ streams. Data can be prepared in a string to mimic the edited screen format. That string could be written to a disk file to preserve the screen image.
An ostringstream object uses a string object to store the output data. The str member function of class ostringstream returns a copy of that string.
Figure 18.11 demonstrates an ostringstream object. The program creates ostringstream object outputString (line 15) and uses the stream insertion operator to output a series of strings and numerical values to the object.
Figure 18.11. Using a dynamically allocated ostringstream object.
(This item is displayed on pages 903 - 904 in the print version)
1 // Fig. 18.11: Fig18_11.cpp 2 // Using a dynamically allocated ostringstream object. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include 8 using std::string; 9 10 #include // header file for string stream processing 11 using std::ostringstream; // stream insertion operators 12 13 int main() 14 { 15 ostringstream outputString; // create ostringstream instance 16 17 string string1( "Output of several data types " ); 18 string string2( "to an ostringstream object:" ); 19 string string3( " double: " ); 20 string string4( " int: " ); 21 string string5( " address of int: " ); 22 23 double double1 = 123.4567; 24 int integer = 22; 25 26 // output strings, double and int to ostringstream outputString 27 outputString << string1 << string2 << string3 << double1 28 << string4 << integer << string5 << &integer; 29 30 // call str to obtain string contents of the ostringstream 31 cout << "outputString contains: " << outputString.str(); 32 33 // add additional characters and call str to output string 34 outputString << " more characters added"; 35 cout << " after additional stream insertions, " 36 << "outputString contains: " << outputString.str() << endl; 37 return 0; 38 } // end main
|
Lines 2728 output string string1, string string2, string string3, double double1, string string4, int integer, string string5 and the address of int integerall to outputString in memory. Line 31 uses the stream insertion operator and the call outputString.str() to display a copy of the string created in lines 2728. Line 34 demonstrates that more data can be appended to the string in memory by simply issuing another stream insertion operation to outputString. Lines 3536 display string outputString after appending additional characters.
An istringstream object inputs data from a string in memory to program variables. Data is stored in an istringstream object as characters. Input from the istringstream object works identically to input from any file. The end of the string is interpreted by the istringstream object as end-of-file.
Figure 18.12 demonstrates input from an istringstream object. Lines 1516 create string input containing the data and istringstream object inputString constructed to contain the data in string input. The string input contains the data
Input test 123 4.7 A
which, when read as input to the program, consist of two strings ("Input" and "test"), an int (123), a double (4.7) and a char ('A'). These characters are extracted to variables string1, string2, integer, double1 and character in line 23.
Figure 18.12. Demonstrating input from an istringstream object.
(This item is displayed on pages 904 - 905 in the print version)
1 // Fig. 18.12: Fig18_12.cpp 2 // Demonstrating input from an istringstream object. 3 #include 4 using std::cout; 5 using std::endl; 6 7 #include 8 using std::string; 9 10 #include 11 using std::istringstream; 12 13 int main() 14 { 15 string input( "Input test 123 4.7 A" ); 16 istringstream inputString( input ); 17 string string1; 18 string string2; 19 int integer; 20 double double1; 21 char character; 22 23 inputString >> string1 >> string2 >> integer >> double1 >> character; 24 25 cout << "The following items were extracted " 26 << "from the istringstream object:" << " string: " << string1 27 << " string: " << string2 << " int: " << integer 28 << " double: " << double1 << " char: " << character; 29 30 // attempt to read from empty stream 31 long value; 32 inputString >> value; 33 34 // test stream results 35 if ( inputString.good() ) 36 cout << " long value is: " << value << endl; 37 else 38 cout << " inputString is empty" << endl; 39 40 return 0; 41 } // end main
|
The data is then output in lines 2528. The program attempts to read from inputString again in line 32. The if condition in line 35 uses function good (Section 15.8) to test if any data remains. Because no data remains, the function returns false and the else part of the if...else statement is executed.
Introduction to Computers, the Internet and World Wide Web
Introduction to C++ Programming
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Functions and an Introduction to Recursion
Arrays and Vectors
Pointers and Pointer-Based Strings
Classes: A Deeper Look, Part 1
Classes: A Deeper Look, Part 2
Operator Overloading; String and Array Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Templates
Stream Input/Output
Exception Handling
File Processing
Class string and String Stream Processing
Web Programming
Searching and Sorting
Data Structures
Bits, Characters, C-Strings and structs
Standard Template Library (STL)
Other Topics
Appendix A. Operator Precedence and Associativity Chart
Appendix B. ASCII Character Set
Appendix C. Fundamental Types
Appendix D. Number Systems
Appendix E. C Legacy Code Topics
Appendix F. Preprocessor
Appendix G. ATM Case Study Code
Appendix H. UML 2: Additional Diagram Types
Appendix I. C++ Internet and Web Resources
Appendix J. Introduction to XHTML
Appendix K. XHTML Special Characters
Appendix L. Using the Visual Studio .NET Debugger
Appendix M. Using the GNU C++ Debugger
Bibliography