Deitel & Associates, Inc. Logo

Back to
digg.png delicious.png blinkit.png furl.png
C++ How to Program, 5/e

© 2005
pages: 1500
Buy the Book!
Amazon logo
InformIT logo

Many applications input information from string objects or dynamically build string objects in memory. This is done primarily to increase application performance. For example, rather than read individual data items from a file, an application can read a chunk of data into a string object, then perform the individual data inputs with the more efficient "in-memory" input operations. Similarly, an application can build a string object in memory that contains many pieces of data, then output that string object in one operation to a file. C++'s string streams facilitate performing in-memory I/O with string objects. This tutorial is intended for students and professionals who are familiar with standard C++ I/O with cin and cout or with C++ file processing.

Download the code examples for this tutorial.

[Note: This tutorial is an excerpt (Section 18.12) of Chapter 18, Class string and String Stream Processing, from our textbook C++ How to Program, 5/e. These tutorials may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., C++ HOW TO PROGRAM, ©2005, pp.902-905. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

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

Page 1 | 2 | 3

Return to Tutorial Index