To retrieve a pointer to the stream buffer.
basic_stringbuf<charT, traits>* rdbuf() const;
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.
A pointer to an object of type basic_stringbuf sb is returned by the rdbuf function.
basic_ostringstream::rdbuf()
basic_ios::rdbuf()
basic_stringstream::rdbuf()
#include <iostream> #include <sstream> #include <string> std::string motto = "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 : CodeWarrior - "Software at Work" The current position is 0 from the beginning The current position is 10 from the beginning The stringbuf array is CodeWarrior CodeWarrior - "Software at Work"