This section describes class strstream, which is a specialization of class iostream.strstream deals with input and output style operations on arrays of bytes. #include <iostream.h> class ios { bin, tabexp}; // and lots of others, see ios ... }; #include <strstream.h> class strstreambase : public virtual ios { public: strstreambuf* }; class istrstream : public strstreambase, public istream { public: istrstream(char*); }; class ostrstream : public strstreambase, public ostream { public:
}; class strstream : public strstreambase, public iostream { public
}; | |||||||||
strstreambase provides the rdbuf() member function. Defining strstreambases is noti ntended. strstream specializes iostream for storing and fetching from arrays of bytes. The streambuf associated with a strstream is a strstreambuf (see sstreambuf). In the following descriptions assume:
Constructorsistrstream(char * cp) Characters are fetched from the (null-terminated) string cp. The terminating null character is not part of the sequence. Seeks (istream::seekg()) are allowed within that array. istrstream(char * cp, int len) Characters are fetched from the array beginning at cp and extending for len bytes.Seeks (istream::seekg()) are allowed anywhere within that array. ostrstream() Space is dynamically allocated to hold stored characters. ostrstream(char * cp, int n, int mode) Characters are stored into the array starting at cp and continuing for n bytes. If ios::ate or ios::app is set in mode, then cp is assumed to be a null-terminated string and storing begins at the null character. Otherwise, storing begins at cp. Seeks are allowed anywhere in the array. strstream() Space is dynamically allocated to hold stored characters. strstream(char * cp, int n, int mode) Characters are stored into the array starting at cp and continuing for n bytes. If ios::ate or ios::app is set in mode, then cp is assumed to be a null-terminated string and storing begins at the null character. Otherwise, storing begins at cp. Seeks are allowed anywhere in the array. strstreambase membersstrstreambuf * ssb = iss.rdbuf() rdbuf() may be used in derived classes only. Returns the strstreambuf connected to iss/oss/ss. ostrstream memberschar * cp=oss.str() Returns a pointer to the array being used and "freezes" the array. Once str has been called the effect of storing more characters into oss is undefined. If oss was constructed with an explicit array, cp is just a pointer to the array. Otherwise, cp points to a dynamically allocated area. Until str is called, deleting the dynamically allocated area is the responsibility of oss. After str returns, the array becomes the responsibility of the user program. int i=oss.pcount() Returns the number of bytes that have been stored into the buffer. This is mainly of use when binary data has been stored and oss.str() does not point to a null terminated string. strstream memberchar * cp=ss.str() Returns a pointer to the array being used and "freezes" the array. Once str() has been called, the effect of storing more characters into ss is undefined. If ss was constructed with an explicit array, cp is just a pointer to the array. Otherwise, cp points to a dynamically allocated area. Until str is called, deleting the dynamically allocated area is the responsibility of ss. After str() returns, the array becomes the responsibility of the user program . | |||||||||
EXAMPLE | The following program defines a string str1, and then reads from the string like an input stream, by using the >> operator. Each character read from the string is printed on cout. #include <iostream.h> #include <strstream.h> const char * const str1 = "A test string to check strstream\n"; /* Use const to make sure that the string, and the pointer */ /* to it, cannot be changed. */ int main() { istrstream is((char*) str1); /* Declare variable is using str1 string */ is.unsetf(ios::skipws); /* By default, an istrstream will skip white space on */ /* input. Change the default behaviour by clearing the */ /* skipws flag so that it will not skip white space on */ /* input. */ while (EOF != is.peek()) { char c; is >> c; /* Note how the text string is accessed like an input */ /* string. */ cout << c; } return 0; } A test string to check strstream % CCM0998 CPU time used: 0.0007 seconds | ||||||||
SEE ALSO | |||||||||