ostringstream

A

Als

What's the meaning of "freeze" as member of ostringstream class? Is it
needed always to call freeze() at the disposal of an ostringstream object?
Is it also needed before disposing an istringsteam object?

Any comment is appreciated.
 
R

Ron Natalie

Als said:
What's the meaning of "freeze" as member of ostringstream class? Is it
needed always to call freeze() at the disposal of an ostringstream object?
Is it also needed before disposing an istringsteam object?
There is no freeze() in stringsream. You're confusing it with the strstream
class. The stringstream manages it's own buffer and passes it in and out
via std::string. strstream uses a char array that is shared with the invoking
code. freeze() locks out further changes by the stream so the invoking code
can manipulate it.
 
A

Als

Ron Natalie said:
There is no freeze() in stringsream. You're confusing it with the strstream
class. The stringstream manages it's own buffer and passes it in and out
via std::string. strstream uses a char array that is shared with the invoking
code. freeze() locks out further changes by the stream so the invoking code
can manipulate it.

Thank you for clarification.

If no freeze() in stringsream. Then what is
ostringstream::rdbuf()->freeze(0);?

Besides passing in and out stringstream via std::string, my understanding is
it can also be via int or other types. Is this correct?

Could you give an example please to show that strstream needs calling
freeze() and stringstream doesn't please?

In the code:

ostringstream o;
o << "hello world" << ends;

What's "ends" for? My book says that "output null in string" but I am not
sure what it means.
 
R

Ron Natalie

Als said:
If no freeze() in stringsream. Then what is
ostringstream::rdbuf()->freeze(0);?

An error, there is no such member in the basic_stringbuf class.
Besides passing in and out stringstream via std::string, my understanding is
it can also be via int or other types. Is this correct?

No. You're confusing two things. A stringstream reads and writes to a
string (as opposed to an fstream that reads and writes to a file).
Could you give an example please to show that strstream needs calling
freeze() and stringstream doesn't please?

Because stringstream HAS no freeze routine to call. When you either
pass in a string to init the buffer or call str() to extract the string from the
buffer, it MAKES a copy. It is therefore not necessary to freeze the
internal buffer because the invoking code has no access to it.

In a strstream you share internal buffer with the calling program. Freezing
it keeps the stringstream from changing it when you don't want that to happen.
In the code:

ostringstream o;
o << "hello world" << ends;

What's "ends" for? My book says that "output null in string" but I am not
sure what it means.

ends writes a null character (i.e., one that is zero valued) to the stream. The
question is whether you really want one. In a (std::)string based stream, nulls
are just another character. For a char* based stream like strstream, you need
the null to signify the end of the data.

I don't know what book you are using but if it uses freeze() with stringstream
it's just wrong and if it uses ends with anything other than a strstream, it's
being stupid.
 
S

Stephen Howe

Could you give an example please to show that strstream needs calling
freeze() and stringstream doesn't please?

In the code:

ostringstream o;
o << "hello world" << ends;

What's "ends" for? My book says that "output null in string" but I am not
sure what it means.

Your book is muddled. Burn it or shred it, it is totally wrong;

In code you might have

ostrstream o;

o << "hello world" << ends;

ostringstream os;

os << "hello world";

ends is shorthand for '\0'. strstream and family require a terminating nul
character as the buffer is a C style string. stringstream and family require
no terminating nul character at all. No mention is made as to what format
the buffer is internally in stringstreams but the interface in terms of
setting and getting the buffer contents is in terms of a std::string.

You can now do

printf("C string %s\n", o.str());
printf("C string %s\n", os.str().c_str());

but the difference is that dynamic strstream's enter a frozen state until
unfrozen (moreover the calling code takes responsibility to delete []
memory if a dynamic frozen strstream goes out of scope) where as
stringstreams have no concept of "frozenness", you can immediately add more
characters to its buffer.

Stephen Howe
 
D

David Harmon

Your book is muddled. Burn it or shred it,

But first, post the author and title of the book and the page number
where the confused example came from!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top