I want to overwrite the string in an std::ostringstream
but std::ends
isn't working. Is this a problem with VS2012, or am I doing something wrong? Here is example code:
std::ostringstream foo;
foo << "1,2,3,4,5,6";
std::cout << foo.str(); // prints: 1,2,3,4,5,6
foo.clear();
foo.seekp( std::ios_base::beg );
foo << "A,B,C" << std::ends;
std::cout << foo.str(); // prints: A,B,C,4,5,6 NOT just: A,B,C
EDIT:
I'm getting a lot of answers that I can use foo.str( std::string() )
to clear the string... I know that. This question is a spin off from here: How to reuse an ostringstream?
I'm trying not to reallocate the buffer.
actually it prints
note the [NUL]
which corresponds to the std::ends
and did overwrote the comma after the 3
the ostringstream::str()
function returns a container which tracks its length and ignores any possible line ending charakter.
so to get a format which respects the string ending character you can e.g. use the char *
(or wchar_t*
if compiling with unicode support) representation obtained via std::string::c_str()
like this foo.str().c_str();