21.12 String Stream Processing

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 <sstream> and <iostream> headers.

Error-Prevention Tip 21.1

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.

 

Error-Prevention Tip 21.2

To assist with data validation, C++11 provides powerful regular-expression capabilities. For example, if a program requires a user to enter a U.S. format telephone number (e.g., (800) 555-1212), you can use a regular-expression pattern to confirm that the user’s input matches the expected format. Many websites provide regular expressions for validating email addresses, URLs, phone numbers, addresses and other popular kinds of data. We introduce regular expressions and provide several examples in Chapter 24.

Software Engineering Observation 21.1

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.3

Demonstrating ostringstream

Figure 21.11 demonstrates an ostringstream object. The program creates ostringstream object outputString (line 9) and uses the stream insertion operator to output a series of strings and numerical values to the object.

Fig. 21.11 Using an ostringstream object.

Alternate View

 1  // Fig. 21.11: Fig21_11.cpp
 2  // Using an ostringstream object.
 3  #include <iostream>
 4  #include <string>
 5  #include <sstream> // header for string stream processing
 6  using namespace std;
 7
 8  int main() {
 9     ostringstream outputString; // create ostringstream object
10
11     string string1{"Output of several data types "};
12     string string2{"to an ostringstream object:"};
13     string string3{"
        double: "};
14     string string4{"
           int: "};
15     string string5{"
address of int: "};
16
17     double double1{123.4567};
18     int integer{22};
19
20     // output strings, double and int to ostringstream outputString
21     outputString << string1 << string2 << string3 << double1       
22        << string4 << integer << string5 << &integer;               
23
24     // call str to obtain string contents of the ostringstream
25     cout << "outputString contains:
" << outputString.str(); 
26
27     // add additional characters and call str to output string
28     outputString << "
more characters added";
29     cout << "

after additional stream insertions,
" 
30        << "outputString contains:
" << outputString.str() << endl;
31  }

outputString contains:
Output of several data types to an ostringstream object:
        double: 123.457
           int: 22
address of int: 0012F540

after additional stream insertions,
outputString contains:
Output of several data types to an ostringstream object:
        double: 123.457
        int: 22
address of int: 0012F540
more characters added

Lines 21–22 output string string1, string string2, string string3, double double1, string string4, int integer, string string5 and the address of int integer—all to outputString in memory. Line 25 uses the stream insertion operator and the call outputString.str() to display a copy of the string created in lines 21–22. Line 28 demonstrates that more data can be appended to the string in memory by simply issuing another stream insertion operation to outputString. Lines 29–30 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.

Demonstrating istringstream

Figure 21.12 demonstrates input from an istringstream object. Lines 9–10 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 17.

Fig. 21.12 Demonstrating input from an istringstream object.

Alternate View

 1  // Fig. 21.12: Fig21_12.cpp
 2  // Demonstrating input from an istringstream object.
 3  #include <iostream>
 4  #include <string>
 5  #include <sstream>
 6  using namespace std;
 7
 8  int main() {
 9     string input{"Input test 123 4.7 A"};
10     istringstream inputString{input};
11     string string1;
12     string string2;
13     int integer;
14     double double1;
15     char character;
16
17     inputString >> string1 >> string2 >> integer >> double1 >> character;
18
19     cout << "The following items were extracted
" 
20        << "from the istringstream object:" << "
string: " << string1
21        << "
string: " << string2 << "
   int: " << integer
22        << "
double: " << double1 << "
 char: " << character;
23
24     // attempt to read from empty stream
25     long value;
26     inputString >> value;
27
28     // test stream results
29     if (inputString.good()) {
30        cout << "

long value is: " << value << endl;
31     }
32     else {
33        cout << "

inputString is empty" << endl;
34     }
35  }

The following items were extracted
from the istringstream object:
string: Input
string: test
   int: 123
double: 4.7
   char: A
inputString is empty

The data is then output in lines 19–22. The program attempts to read from inputString again in line 26. The if condition in line 29 uses function good (Section 13.8) to test if any data remains. Because no data remains, the function returns false and the else part of the ifelse statement executes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset