views:

94

answers:

4

On MSVC 2005, I have the following code.

std::ostringstream stream("initial string ");
stream << 5;
std::cout << stream.str();

What I expect is:

initial string 5

What I get is:

5nitial string

Initializing the stream with a string, I would expect the stream to move its position to the end of the initial string. Obviously, STL doesn't agree with me (not the first time).

What's the purpose of such behavior? Is this use case useful for anything? Also, is there a way to advance the stream position to the end of the initial string?

+3  A: 

To seek to the end, do this:

stream.seekp(0, ios_base::end);

As to why the write position defaults to the front, I don't know.

RichieHindle
+6  A: 

You can specify the second (optional) parameter of the constructor to set the stream cursor at the end:

std::ostringstream stream("initial string ", std::ios::ate);
AraK
+9  A: 

Using an ostringstream and providing an initial value is just like opening an existing file for writing. You have some data there, and unless you specify otherwise, your initial position will be at the beginning of the data. You can seek in the data, or you specify ios::ate to initially position the write pointer to the end of the existing data, or you can specify ios::app to always position the write pointer to the end of the existing data.

Jerry Coffin
This makes sense. `ostringstream` conforms with other stream types and defaults to the beginning until explicitly moved elsewhere. Thanks.
Jesse Stimpson
That is so obvious now that you explain it in relation to files, but before I always though the behavior was very odd. My new cool fact of the day.
Martin York
A: 

Imagine what would happen if you have the default cursor at the end:

ostringstream stream("abcdef|"); // cursor position at |
cout << stream.str(); // writes nothing !!!

instead, since the cursor position is at the front:

ostringstream stream("|abcdef"); // cursor position at |
cout << stream.str(); // writes "abcdef" and moves the cursor to "abcdef|"

ostringstream is meant to be constructed once, then never <<-written; or constructed empty, then <<-written. If you construct it with the string constructor, then <<-writes something, then the result is as you've seen.

Lie Ryan
Something doesn't add up for me here. If what you say is true, then wouldn't `std::ostringstream stream("initial string ", std::ios::ate);` print nothing since `ate` moves the "cursor" to the end? That's not the case with my compiler.
Jesse Stimpson
This is incorrect. What's in the buffer has nothing to do with where you'll be writing to next.
GMan