Skip to content. Skip to navigation

ICTP Portal

Sections
You are here: Home Manuals on-line PGI Compiler pgC_lib stdlibug 2.6 In-Memory Input/Output
Personal tools
Document Actions

2.6 In-Memory Input/Output



Click on the banner to return to the user guide home page.

2.6 In-Memory Input/Output

The iostreams facility supports not only input and output to external devices like files. It also allows in-memory parsing and formatting. Source and sink of the characters read or written becomes a string held somewhere in memory. You use in-memory I/O if the information to be read is already available in the form of a string, or if the formatted result will be processed as a string. For example, to interpret the contents of the string argv[1] as an integer value, the code might look like this:

int i;
if (istringstream(argv[1]) >> i)                             //1
   // use the value of i
//1The parameter of the input string stream constructor is a string; here a character array, namely argv[1], is provided as an argument and is implicitly converted to a string. From this newly constructed input string stream, which contains argv[1], an integer value is extracted.

The inverse operation, taking a value and converting it to characters that are stored in a string, might look like this:

struct date {
  int day,month,year;
} today = {8,4,1996};
ostringstream ostr;                                           //1
ostr << today.month << '-' << today.day <<'-' << today.year;  //2
if (ostr)
   display(ostr.str());                                       //3
//1An output string stream is allocated.
//2Values are inserted into the output string stream.
//3The result of the formatting can be retrieved in the form of a string, which is returned by str().

As with file streams, there are three class templates that implement string streams: basic_istringstream <charT,traits,Allocator>, basic_ostringstream <charT,traits,Allocator>, and basic_stringstream <charT,traits,Allocator>. These are derived from the stream base classes, basic_istream <charT, traits>, basic_ostream <charT, traits>, and basic_iostream <charT, traits>. Therefore they inherit all the functions for formatted input and output described in Section 2.3, as well as the stream state. They also have functions for setting and retrieving the string that serves as source or sink, and constructors that allow you to set the string before construction time. For convenience, there are the regular typedefs istringstream, ostringstream, and stringstream, with wistringstream, wostringstream, and wstringstream for the respective tiny and wide character string streams.

The buffering is done through a specialized stream buffer class, basic_stringbuf <charT,traits,Allocator>.

2.6.1 The Internal Buffer

String streams can take a string, provided either as an argument to the constructor, or set later through the str(const basic_string <charT, traits, Allocator>&) function. This string is copied into an internal buffer, and serves as source or sink of characters to subsequent insertions or extractions. Each time the string is retrieved through the str() function, a copy of the internal buffer is created and returned.

Output string streams are dynamic. The internal buffer is allocated once an output string stream is constructed. The buffer is automatically extended during insertion each time the internal buffer is full.

Input string streams are always static. You can extract as many items as are available in the string you provided the string stream.

2.6.2 The Open Modes

The only open modes that have an effect on string streams are in, out, ate, and app. They have more or less the same meaning that they have with file streams (see section 2.5.4).

The binary open mode is irrelevant, because there is no conversion to and from the dependent file format of the operating system. The trunc open mode is simply ignored.


©Copyright 1996, Rogue Wave Software, Inc.


Powered by Plone This site conforms to the following standards: