Randomly move to a position in an output stream.
basic_ostream<charT, traits>& seekp(pos_type);
basic_ostream<charT, traits>& seekp
(off_type, iosbase::seekdir);
The function seekp is overloaded to take a single argument of a pos_type pos that calls rdbuf()->pubseekpos(pos). It is also overloaded to take two arguments: an off_type off and i os_base::seekdir type dir that calls rdbuf()->pubseekoff(off, dir).
Returns the this pointer.
basic_istream::seekg , basic_ostream::tellp
#include <iostream> #include <sstream> #include <string> std::string motto = "CodeWarrior - Software at Work"; int main() { using namespace std; ostringstream ostr(motto); streampos cur_pos, start_pos; 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 " << cur_pos.offset() << " from the beginning\n"; ostr.seekp(str_off); cur_pos = ostr.tellp(); cout << "The current position is " << cur_pos.offset() << " 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 The ostringstream array is still CodeWarrior - Software at Work