From: Piranha on
I found some basic info about the topic elsewhere, just enough to
understand what it does, but not enough to understand why.

I can feed a lot of different stuff into a stringstream, and the first
question is, why or how does stringstream get around the problem with
different types of variables?

A stringstream is no string, to convert it to char, one has to
construct something like stringstream.str().c_str() so what format
does the data have in a stringstream?

And then there are istringstream and ostringstream, where I am not
really sure about the exact differences between the types.

I found a lot of information how to feed a stringstream, but nothing
how one could overwrite or delete its content, besides simply using a
new, fresh one.

Could anyone give me a link where this is explained or write me a few
notes what this is all about? Can you tell from the question already?
I need an explanation for beginners.
From: Jerry Coffin on
In article <b24156e8-a07a-4069-a81f-f2e9f7f6dad6
@k39g2000hsf.googlegroups.com>, eu_piranha(a)gmx.net says...

[ ... ]

> I can feed a lot of different stuff into a stringstream, and the first
> question is, why or how does stringstream get around the problem with
> different types of variables?

What exact problem is that? A stringstream is a stream, so it uses the
usual operator<< and operator>> (along with other functions that work
with streams like std::getline). Rather than attempting to get around
different types of variables, it USES different types of variables in
the form of operator overloading, to convert each type of variable in
its preferred fashion.

> A stringstream is no string, to convert it to char, one has to
> construct something like stringstream.str().c_str() so what format
> does the data have in a stringstream?

Internally, a stringstream uses an std::string to store the data. You
can get access to that string with stream.str(). Theoretically, the
stream doesn't really have to use a string to store data internally --
it just has to act like it does, taking and returning strings in
response to the appropriate function calls. I believe they all really do
use strings though.

> And then there are istringstream and ostringstream, where I am not
> really sure about the exact differences between the types.

Just like the difference between an ostream and an istream: an ostream
is strictly for output and an istream is strictly for input. For
example, if you want to format some data into a string, you'd use an
ostringstream. If you have some data in a string and want to read from
it, you can use an istringstream.

> I found a lot of information how to feed a stringstream, but nothing
> how one could overwrite or delete its content, besides simply using a
> new, fresh one.

That's the usual way if you want to overwrite all its content. If (for
example) you have something like records being stored in the stream, you
can use seekp to move the put pointer to an arbitrary spot and overwrite
existing data (just like you would with a file on disk, for example).

--
Later,
Jerry.

The universe is a figment of its own imagination.
From: Daniel T. on
Piranha <eu_piranha(a)gmx.net> wrote:

> I found some basic info about the topic elsewhere, just enough to
> understand what it does, but not enough to understand why.
>
> I can feed a lot of different stuff into a stringstream, and the first
> question is, why or how does stringstream get around the problem with
> different types of variables?

Just like streams do, lot's of overloads. There is a different
operator>> for each of the basic types, for example.

> A stringstream is no string, to convert it to char, one has to
> construct something like stringstream.str().c_str() so what format
> does the data have in a stringstream?

It is an allocated buffer. Probably something like a vector. BTW,
stringstream.str().c_str() is dangerious. str() returns a temporary
object which is destroyed at the next sequence point, so the const char*
returned by c_str() doesn't live very long at all.

> And then there are istringstream and ostringstream, where I am not
> really sure about the exact differences between the types.

istringstreams are only for input, ostringstreams are only for output,
stringstreams can handle both input and output. I.E., istringstream only
has operator>> while ostringstream has operator<<, stringstream has both.

> I found a lot of information how to feed a stringstream, but nothing
> how one could overwrite or delete its content, besides simply using a
> new, fresh one.

a stringstream works very much like a file stream, so overwriting and
deleting is done the same way. It also has the str(const char*)
member-function mystream.str("") will clear it out.

> Could anyone give me a link where this is explained or write me a few
> notes what this is all about? Can you tell from the question already?
> I need an explanation for beginners.

http://www.dinkumware.com/manuals/default.aspx is one of the better
reference sites IMO.

Book reviews for beginners can be found here:
http://www.accu.informika.ru/accu/bookreviews/public/reviews/0sb/beginner
_s_c__.htm
From: Ulrich Eckhardt on
Piranha wrote:
> I found some basic info about the topic elsewhere, just enough to
> understand what it does, but not enough to understand why.
>
> I can feed a lot of different stuff into a stringstream, and the first
> question is, why or how does stringstream get around the problem with
> different types of variables?

It doesn't, that's what the baseclass does. That's why the following code
works:

void write_smiley( std::ostream& out) {
out << ":)\n";
}

// write to console
write_smiley(std::cout);
// write to a file
std::ofstream out("smiley.text");
write_smiley(out);
// write to a memory
std::ostringstream str;
write_smiley(str);
std::cout << "str contained " << str.str();


Anyway, concerning your subject, an IOStream is just a tool for
formatting/parsing and output/input. It uses an attached std::streambuf to
do the actual exchange of characters (IO) while doing the formatting and
parsing itself. In the case of std::fstream, the streambuffer (of type
std::filebuf) performs IO to a file. In the case of std::stringstream, the
streambuffer (of type stringstreambuf, IIRC) performs IO to a std::string.

Note that both std::fstream and std::stringstream only derive from
std::iostream, adding and attaching their according streambuffers to the
baseclass which does the formatting and parsing. You can also do this
manually, using the rdbuf() function.

> A stringstream is no string, to convert it to char, one has to
> construct something like stringstream.str().c_str() so what format
> does the data have in a stringstream?

You shouldn't care. You can get a std::string via the str() member and
that's it. Even worse, your above chain of calls to str() and c_str() is
dangerous, because str() returns a temporary and c_str() then returns a
pointer to memory inside that temporary. However, with the next line of
code that temporary is destroyed, leaving you with a pointer to nowhere!

As a general advise, don't use 'char*' for handling strings (or raw pointers
in general) unless you have a good reason. Since you are a beginner, you
don't have a good reason. ;)

> And then there are istringstream and ostringstream, where I am not
> really sure about the exact differences between the types.

i=input, o=output - these are only usable for either kind of direction, just
like std::ifstream and std::ofstream or std::istream and std::ostream
generally, which also form the baseclasses of the two mentioned groups.

> I found a lot of information how to feed a stringstream, but nothing
> how one could overwrite or delete its content, besides simply using a
> new, fresh one.

That's what is done normally, but there is the overloaded str() function,
too.

Uli

From: Piranha on
On 21 Jan., 00:05, Piranha <eu_pira...(a)gmx.net> wrote:
> I found some basic info about the topic elsewhere, just enough to
> understand what it does, but not enough to understand why.
>
> I can feed a lot of different stuff into a stringstream, and the first
> question is, why or how does stringstream get around the problem with
> different types of variables?
>
> A stringstream is no string, to convert it to char, one has to
> construct something like stringstream.str().c_str() so what format
> does the data have in a stringstream?
>
> And then there are istringstream and ostringstream, where I am not
> really sure about the exact differences between the types.
>
> I found a lot of information how to feed a stringstream, but nothing
> how one could overwrite or delete its content, besides simply using a
> new, fresh one.
>
> Could anyone give me a link where this is explained or write me a few
> notes what this is all about? Can you tell from the question already?
> I need an explanation for beginners.

Thanks to everyone, this is great help.