What is the difference between ostream and ostringstream? When would you use one versus the other?
-
also ostringstream is a derived class from ostream – mlemay Aug 21 '12 at 12:06
4 Answers
Put succinctly: ostringstream
provides a streambuf
, ostream
requires the user to provide one.
To understand the implications, it's necessary to understand a little
how streams work, and I'm not sure that there's a good explanation of this on the Web. The basic abstraction
of ostream
is formatting textual output. You give it an int
or a
double
(or a user defined type—more on that later), and it
convert it into a stream of characters, of type char
. What it does
with that stream depends on the streambuf
which is attached to it;
this is an example of the strategy pattern, where streambuf
is an
abstract base class of the strategy[1]. The standard provides two
implementations of streambuf
, filebuf
and stringbuf
; in practice,
in all but the most trivial applications, you'll probably have some that
you implement yourself.
When outputting, you always use ostream
; it's the class over which the
<<
operators are defined. You're formatting your data into a stream
of characters, and you don't really care where the stream ends up.
When creating an instance: if you create an ostream
, you must provide
it with a streambuf
yourself. More often, you'll create an
ofstream
or an ostringstream
. These are both "convenience" classes,
which derive from ostream
, and provide a streambuf
for it (filebuf
and stringbuf
, as it happens). Practically speaking, all they do is
provide the necessary streambuf
(which affects the constructor and the
destructor, and not very much else); in the case of ofstream
, there
are also a few extra functions which forward to additional functions in
the filebuf
interface.
It's usual (but by no means required) when you define your own
streambuf
to provide convenience overloads of ostream
(and
istream
, if relevant), along the same lines as ofstream
or
ostringstream
.
By the same token, when creating an instance, it's usual to use one of
the "convenience" derived classes, rather than to use ostream
directly
and provide your own streambuf.
And if all of this seems complicated: the iostream classes use just
about all of the facilities of C++ (virtual functions, templates and
function overloading all play an important role). If you're just
learning C++, don't worry too much about it: just use ofstream
or
ostringstream
when you construct an instance, but pass around
references to ostream
. And as you learn about techniques like virtual
functions, templates and operator overloading, return to the iostreams
to understand the role they play in making code more flexible.
[1] For various reasons, std::streambuf
is not actually abstract. But
the implementations of the virtual functions in it are useless;
extraction always returns EOF, and insertion always fails.

- 2,352
- 5
- 23
- 38

- 150,581
- 18
- 184
- 329
Here is nice view of the Inheritance Hierarchy for C++ Stream Classes :)
This article at the section
3.1 ofstream and ostringstream
has what you need.
In essence : The ofstream
class makes it possible to write data to files using stream operations
and the ostringstream
class makes it possible to write to strings
.

- 2,505
- 1
- 26
- 28
-
3There are some (obvious) errors at the bottom of your hierarchy: the classes which derive from `iostream` are `fstream` and `stringstream`. (In practice, of course, `fstream` and `stringstream` are almost never used.) – James Kanze Aug 21 '12 at 13:55
ostream
is more general (subclasses support writing to different places), ostringstream
is a specific one writing to a string

- 1,875
- 11
- 22
ostream
has no rdbuf
implementation behind it, whereas ostringstream
uses a stringbuf. Tried documentation?

- 138,757
- 24
- 193
- 173
-
3Sorry... I did look at the documentation but I am still new to C++. I have seen various uses of ostream and ostringstream but I was not sure of the difference. Seems like my question is too basic for this site. Maybe I'll withdraw it. – kmccoy Aug 21 '12 at 12:13
-
It's not about question being too basic,it's more about the answer being readily available. I'm glad to hear you actually *have* tried looking for an answer. – Michael Krelin - hacker Aug 21 '12 at 12:14
-
2Of course I googled... but reading a documentation page like http://www.cplusplus.com/reference/iostream/ostringstream/ isn't straightforward to someone new to C++. Plus, the second question on when one is used over the other has been mostly ignored by the other answers. – kmccoy Aug 21 '12 at 12:17
-
Because the difference in functionality should put you on the right track regarding the use. `ostringstream` is used when you need stream stuff into `string`, whereas `ostream` is mostly used as a type for a parameter (referenced) when the callee is stream implementation agnostic. – Michael Krelin - hacker Aug 21 '12 at 12:20
-
Err... the page you link says "ostringstream provides an interface to manipulate strings as if they were output streams". The very first sentence. Is it not straightforward? – Michael Krelin - hacker Aug 21 '12 at 12:21
-
2And "ostream objects are stream objects used to write and format output as sequences of characters". I didn't understand what an output stream was at the time... but I have read much more about C++ since and got it now. – kmccoy Dec 19 '12 at 11:05