This chapter discusses string-based streams in the standard C++ library.
There are four template classes and 6 various types defined in
the header <sstream> that are used to associate stream buffers with objects of class basic_string.
The sections in this chapter are:
Overview:
The header <sstream> includes classes and typed that associate stream buffers with
string objects for input and output manipulations.
Prototype:
namespace std{
template
<class charT, class traits = char_traits<charT> >
class basic_stringbuf;
typedef basic_stringbuf<char> stringbuf;
typedef basic_strngbuf<wchar> wstringbuf;
template
<class charT, class traits = char_traits<charT> >
class basic_istringstream;
typedef basic_istringstream<char> istringstream;
typedef basic_istringstream<wchar> wistringstream;
template
<class charT, class traits = char_traits<charT> >
class basic_ostringstream;
typedef basic_ostringstream<char> ostringstream;
typedef basic_ostringstream<wchar> wostringstream;
};
}
The class basic_string is discussed in previous chapters.
Overview:
The template class basic_stringbuf is derived from basic_streambuf is use to associate both input and output streams with an object
of class basic_string.
The other topics in this section are:
template
<class charT, class traits = char_traits<charT> >
class basic_stringbuf: public basic_streambuf<charT, traits> > {
public:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
explicit basic_stringbuf
(ios_base::openmode which = ios_base::in | ios_base:out);
explicit basic_stringbuf
(const basic_string<char_type> &str,
ios_base::openmode which = ios_base::in | ios_base:out);
basic_string<char_type> str() const;
void str(const basic_string<char_type>&s);
protected
virtual int_type underflow();
virtual int_type pbackfail
(int_type c = traits::eof());
virtual int_type overflow
(int_type c = traits::eof());
virtual pos_type seekoff
(off_type off,
ios_base::seekdir way,
ios_base::openmode which = ios_base::in | ios_base::out);
virtual pos_type seekpos
(pos_type sp,
ios_base::openmode which = ios_base::in | ios_base::out);
private:
ios_base::openmode mode; exposition only };
}
Remarks: basic_stringbuf is derived from basic_streambuf to associate a stream with a basic_string object for in-core memory character manipulations.
The basic_stringbuf has two constructors:
explicit basic_stringbuf(ios_base::openmode);
explicit basic_stringbuf(const basic_string, ios_base::openmode);To create a string buffer for characters for input/output.
Prototype:
explicit basic_stringbuf
(ios_base::openmode which =
ios_base::in | ios_base:out);
explicit basic_stringbuf
(const basic_string <char_type> &str,
ios_base::openmode which =
ios_base::in | ios_base:out);
Remarks:
The basic_stringbuf constructor is used to create an object usually as an intermediate
storage object for input and output. The overloaded constructor
is used to determine the input or output attributes of the basic_string object when it is created.
Example of basic_stringbuf::basic_stringbuf() usage::
#include <iostream>
#include <sstream>
const int size = 100;
int main()
{
using namespace std;
stringbuf strbuf;
strbuf.pubsetbuf('\0', size);
strbuf.sputn("ABCDE",50);
char ch;
// look ahead at the next character
ch =strbuf.snextc();
cout << ch;
// get pointer was not returned after peeking
ch = strbuf.snextc();
cout << ch;
return 0;
}
Result:
BC
27.7.1.2 Member functions
The class basic_stringbuf has one member functions:
To return or clear the basic_string object stored in the buffer.
Prototype:
basic_string<char_type> str() const;
void str(const basic_string<char_type>&s);
Remarks:
The function str() freezes the buffer then returns a basic_string object.
The function str(const string s) assigns the value of the string `s' to the stringbuf object.
Return:
The no argument version returns a basic_string if successful. The function with an argument has no return.
Example of basic_stringbuf::str() usage::
#include <iostream>
#include <sstream>
#include <cstring>
char CW[] = "Metrowerks CodeWarrior";
char AW[] = " - \"Software at Work\"";
int main()
{
using namespace std;
string buf;
stringbuf strbuf(buf, ios::in | ios::out);
int size;
size = strlen(CW);
strbuf.sputn(CW, size);
size = strlen(AW);
strbuf.sputn(AW, size);
cout << strbuf.str() << endl;
// Clear the buffer then fill it with
// new information and then display it
string clrBuf = "";
string ANewLine = "We Listen we Act";
strbuf.str(clrBuf);
strbuf.sputn( ANewLine.c_str(), ANewLine.size());
cout << strbuf.str() << endl;
return 0;
}
Results
Metrowerks CodeWarrior - "Software at Work"
We Listen we Act
27.7.1.3 Overridden virtual functions
The base class basic_streambuf has several virtual functions that are to be overloaded by derived
classes. The are:
To show an underflow condition and not increment the get pointer.
Prototype:
virtual int_type underflow();
Remarks:
The function underflow overrides the basic_streambuf virtual function.
Return:
The first character of the pending sequence and does not increment
the get pointer. If the position is null returns traits::eof() to indicate failure.
See Also:
To show a failure in a put back operation.
Prototype:
virtual int_type pbackfail
(int_type c = traits::eof());
Remarks:
The function pbackfail overrides the basic_streambuf virtual function.
Return:
The function pbackfail() is only called when a put back operation
really has failed and returns traits::eof. If success occurs the
return is undefined.
See Also:
Consumes the pending characters of an output sequence.
Prototype:
virtual int_type overflow
(int_type c = traits::eof());
Remarks:
The function overflow overrides the basic_streambuf virtual function.
Return:
The function returns traits::eof() for failure or some unspecified result to indicate success.
See Also:
To return an offset of the current pointer in an input or output
streams.
Prototype:
virtual pos_type seekoff
(off_type off,
ios_base::seekdir way,
ios_base::openmode which =
ios_base::in | ios_base::out);
Remarks:
The function seekoff overrides the basic_streambuf virtual function.
Return:
A pos_type value, which is an invalid stream position.
See Also:
To alter an input or output stream position.
Prototype:
virtual pos_type seekpos
(pos_type sp,
ios_base::openmode which =
ios_base::in | ios_base::out);
Remarks:
The function seekoff overrides the basic_streambuf virtual function.
Return:
A pos_type value, which is an invalid stream position.
See Also:
Overview:
The template class basic_istringstream is derived from basic_istream and is use to associate input streams with an object of class basic_string.
The prototype is listed below. The other topics in this section
are:
namespace std {
template
<class charT, class traits = char_traits<charT> >
class basic_istringstream : public basic_istream<charT, traits>
{
public:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
explicit basic_istringstream
(ios_base::openmode which = ios_base::in);
explicit basic_istringstream
(const basic_string<charT> &str,
ios_base::openmode which = ios_base::in);
basic_stringbuf<charT, traits>* rdbuf() const;
basic_strng<charT> str() const;
void str(const basic_string<charT> &s);
private:
basic_stringbuf<charT,traits> sb; exposition only };
}
The class basic_istringstream uses an object of type basic_stringbuf to control the associated storage.
See Also:
basic_ostringstream, basic_string,
basic_stringstream, basic_filebuf.
The class basic_istringstream has two constructors.
basic_istringstream
(ios_base::openmode)
basic_istringstream
(const basic_string, ios_base::openmode)
The basic_istringstream constructors create a basic_stringstream object and initialize the basic_streambuf object.
Prototype:
explicit basic_istringstream
(ios_base::openmode which = ios_base::in);
explicit basic_istringstream
(const basic_string<charT> &str,
ios_base::openmode which = ios_base::in);
Remarks:
The basic_istringstream constructor is overloaded to accept a an object of class basic_string
for input.
See Also:
basic_ostringstream, basic_stringstream
Example of basic_istringsteam::basic_istringsteam() usage:
#include <iostream>
#include <string>
#include <sstream>
int main()
{
using namespace std;
string sBuffer = "3 12.3 line";
int num = 0;
double flt = 0;
char szArr[20] = "\0";
istringstream Paragraph(sBuffer, ios::in);
Paragraph >> num;
Paragraph >> flt;
Paragraph >> szArr;
cout << num << " " << flt << " "
<< szArr << endl;
return 0;
}
Result
3 12.3 line
27.7.2.2 Member functions
The class basic_istringstream has two member functions
To retrieve a pointer to the stream buffer.
Prototype:
basic_stringbuf<charT, traits>* rdbuf() const;
Remarks:
To manipulate a stream for random access or synchronization it
is necessary to retrieve a pointer to the streams buffer. The
function rdbuf() allows you to retrieve this pointer.
Return:
A pointer to an object of type basic_stringbuf sb is returned by the rdbuf function.
See Also:
Example of basic_istringstream::rdbuf() usage.:
#include <iostream>
#include <sstream>
std::string buf = "Metrowerks CodeWarrior - \"Software at work\"";
char words[50];
int main()
{
using namespace std;
istringstream ist(buf);
istream in(ist.rdbuf());
in.seekg(25);
in.get(words,50);
cout << words;
return 0
}
Result
"Software at work"
basic_istringstream::str
To return or assign the basic_string object stored in the buffer.
Prototype:
basic_string<charT> str() const;
void str(const basic_string<charT> &s);
Remarks:
The function str() freezes the buffer then returns a basic_string object.
The function str(const string s) assigns the value of the string `s' to the stringbuf object.
Return:
The no argument version returns a basic_string if successful. The function with an argument has no return.
See Also:
Example of basic_istringstream::str() usage.:
#include <iostream>
#include <sstream>
std::string buf = "Metrowerks CodeWarrior - \"Software at Work\"";
int main()
{
using namespace std;
istringstream istr(buf);
cout << istr.str();
return 0;
}
Result:
Metrowerks CodeWarrior - "Software at Work"
27.7.2.3 Class basic_ostringstream
Overview:
The template class basic_ostringstream is derived from basic_ostream is use to associate output streams with an object of class basic_string.
The prototype is listed below. The other topics in this section
are:
Prototype:
namespace std {
template
<class charT, class traits = char_traits<charT> >
class basic_ostringstream : public basic_ostream<charT, traits>{
public:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
explicit basic_ostringstream
(ios_base::openmode which = ios_base::out);
explicit basic_ostringstream
(const basic_string<charT> &str,
ios_base::openmode which = ios_base::out);
basic_stringbuf<charT, traits>* rdbuf() const;
basic_strng<charT> str() const;
void str(const basic_string<charT> &s);
private:
basic_stringbuf<charT,traits> sb; exposition only };
}
The class basic_ostringstream uses an object of type basic_stringbuf to control the associated storage.
See Also:
basic_istringstream, basic_string,
basic_stringstream, basic_filebuf.
The class basic_ostringstream has two constructors
basic_ostringstream
(ios_base::openmode)
basic_ostringstream
(const basic_string, ios_base::openmode
The basic_ostringstream constructors create a basic_stringstream object and initialize the basic_streambuf object.
Prototype:
explicit basic_ostringstream
(ios_base::openmode which = ios_base::out);
explicit basic_ostringstream
(const basic_string<charT> &str,
ios_base::openmode which = ios_base::out);
Remarks:
The basic_stringstream constructor is overloaded to accept a an object of class basic_string
for output.
See Also:
basic_istringstream, basic_stringstream
Example of basic_ostringsteam::basic_ostringsteam() usage:
The file MW Reference contains Metrowerks CodeWarrior - "Software at Work" Registered Trademark
#include <iostream> #include <fstream> #include <sstream> #include <cstdlib> int main() { using namespace std; ifstream in("MW Reference"); if(!in.is_open()) {cout << "can't open file for input"; exit(1);} ostringstream Paragraph; char ch ='\0'; while((ch = in.get()) != EOF) { Paragraph << ch; } cout << Paragraph.str(); in.close(); return 0; }
Result: Metrowerks CodeWarrior - "Software at Work" Registered Trademark
27.7.2.5 Member functions
The class
basic_ostringstreamhas two member functions:
To retrieve a pointer to the stream buffer.
Prototype:
basic_stringbuf<charT, traits>* rdbuf() const;
Remarks:
To manipulate a stream for random access or synchronization it
is necessary to retrieve a pointer to the streams buffer. The
function rdbuf() allows you to retrieve this pointer.
Return:
A pointer to an object of type basic_stringbuf sb is returned by the rdbuf function.
See Also:
example of basic_ostringsteam::rdbuf() usage:
#include <iostream>
#include <sstream>
#include <string>
std::string motto = "Metrowerks CodeWarrior - \"Software at Work\"";
int main()
{
using namespace std;
ostringstream ostr(motto);
streampos cur_pos(0), start_pos(0);
cout << "The original array was :\n"
<< motto << "\n\n";
// associate buffer
stringbuf *strbuf(ostr.rdbuf());
streamoff str_off = 10;
cur_pos = ostr.tellp();
cout << "The current position is "
<< static_cast<streamoff>(cur_pos);
<< " from the beginning\n";
ostr.seekp(str_off);
cur_pos = ostr.tellp();
cout << "The current position is "
<< static_cast<streamoff>(cur_pos);
<< " from the beginning\n";
strbuf->sputc('\0');
cout << "The stringbuf array is\n"
<< strbuf->str() << "\n\n";
cout << "The ostringstream array is still\n"
<< motto;
return 0;
}
Results:
The original array was :
Metrowerks CodeWarrior - "Software at Work"
The current position is 0 from the beginning
The current position is 10 from the beginning
The stringbuf array is
Metrowerks
Metrowerks CodeWarrior - "Software at Work"
basic_ostringstream::str
To return or assign the basic_string object stored in the buffer.
Prototype:
basic_strng<charT> str() const;
void str(const basic_string<charT> &s);
Remarks:
The function str() freezes the buffer then returns a basic_string object.
The function str(const string s) assigns the value of the string `s' to the stringbuf object.
Return:
The no argument version returns a basic_string if successful. The function with an argument has no return.
See Also:
basic_streambuf::str(), basic_istringstream.str()
Example of basic_ostringstream::str() usage.:
#include <iostream>
#include <sstream>
int main()
{
using namespace std;
ostringstream out;
out << "Ask the teacher anything\n";
out << "OK, what is 2 + 2?\n";
out << 2 << " plus " << 2 << " equals "
<< 4 << ends;
cout << out.str();
return 0;
}
Result:
Ask the teacher anything
OK, what is 2 + 2?
2 plus 2 equals 4?
27.7.3 Class basic_stringstream
Overview:
The template class basic_stringstream is derived from basic_iostream is use to associate input and output streams with an object of class basic_string.
The prototype is listed below. The other topics in this section
are:
Prototype:
namespace std {
template
<class charT, class traits = char_traits<charT> >
class basic_stringstream : public basic_iostream<charT, traits>
{
public:
typedef charT char_type;
typedef typename traits::int_type int_type;
typedef typename traits::pos_type pos_type;
typedef typename traits::off_type off_type;
explicit basic_stringstream
(ios_base::openmode which = ios_base::out | ios_base::out);
explicit basic_stringstream
(const basic_string<charT> &str,
ios_base::openmode which = ios_base::out | ios_base::out);
basic_stringbuf<charT, traits>* rdbuf() const;
basic_strng<charT> str() const;
void str(const basic_string<charT> &s);
private:
basic_stringbuf<charT,traits> sb; exposition only };
}
The class basic_stringstream uses an object of type basic_stringbuf to control the associated storage.
See Also:
basic_istringstream, basic_string, basic_stringstream, basic_filebuf
The class basic_stringstream has two constructors:
explicit basic_stringstream
(ios_base::openmode)
explicit basic_stringstream
(const basic_string, ios_base::openmode)
The basic_stringstream constructors create a basic_stringstream object and initialize the basic_streambuf object.
Prototype:
explicit basic_stringstream
(ios_base::openmode which =
ios_base::out | ios_base::out);
explicit basic_stringstream
(const basic_string<charT> &str,
ios_base::openmode which =
ios_base::out | ios_base::out);
Remarks:
The basic_stringstream constructor is overloaded to accept a an object of class basic_string
for input or output.
See Also:
basic_ostringstream, basic_istringstream
Example of basic_stringstream::basic_stringstream() usage:
#include <iostream>
#include <sstream>
char buf[50] = "ABCD 22 33.33";
char words[50];
int main()
{
using namespace std;
stringstream iost;
char word[20];
long num;
double real;
iost << buf;
iost >> word;
iost >> num;
iost >> real;
cout << word << " "
<< num << " "
<< real << endl;
return 0;
}
Result
ABCD 22 33.33
27.7.3.5 Member functions
The class basic_stringstream has two member functions:
To retrieve a pointer to the stream buffer.
Prototype:
basic_stringbuf<charT, traits>* rdbuf() const;
Remarks:
To manipulate a stream for random access or synchronization it
is necessary to retrieve a pointer to the streams buffer. The
function rdbuf() allows you to retrieve this pointer.
Return:
A pointer to an object of type basic_stringbuf sb is returned by the rdbuf function.
See Also:
basic_ostringstream::rdbuf() basic_ios::rdbuf()
basic_stringstream::rdbuf()
Example of basic_stringstream::rdbuf() usage:
#include <iostream>
#include <iostream>
#include <sstream>
std::string buf = "Metrowerks CodeWarrior - \"Software at Work\"";
char words[50];
int main()
{
using namespace std;
stringstream ist(buf, ios::in);
istream in(ist.rdbuf());
in.seekg(25);
in.get(words,50);
cout << words;
return 0;
}
Result
"Software at Work"
basic_stringstream::str
To return or assugn the basic_string object stored in the buffer.
Prototype:
basic_strng<charT> str() const;
void str(const basic_string<charT> &s);
Remarks:
The function str() freezes the buffer then returns a basic_string object.
The function str(const string s) assigns the value of the string `s' to the stringbuf object.
Return:
The no argument version returns a basic_string if successful. The function with an argument has no return.
See Also:
Example of basic_stringstream::str() usage:
#include <iostream>
#include <sstream>
std::string buf = "Metrowerks CodeWarrior - \"Software at Work\"";
char words[50];
int main()
{
using namespace std;
stringstream iost(buf, ios::in);
cout << iost.str();
return 0;
}
Result
Metrowerks CodeWarrior - "Software at Work"
[ First ] [ Previous ] [ Next ] [ Last ] [ Manuals ]
Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.
Last updated: July 21, 2000