ostringstream, ends: what am I missing



 DEVELOP > c-Plus-Plus > ostringstream, ends: what am I missing

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "jay"
Date: 21 Dec 2003 06:05:01 AM
Object: ostringstream, ends: what am I missing
I was told that an ostringstream should be terminated by ends.
However, the following code snippet does not do what I would expect if
that were the case:
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
ostringstream oss;
string myString;
long aCounter = 0;

oss << ++aCounter << ends;
myString = oss.str() + "_SUFFIX";
cout << myString << endl; // output: "1 _SUFFIX", unexpected

oss.str("");

oss << ++aCounter;
myString = oss.str() + "_SUFFIX";
cout << myString << endl; // output: "2_SUFFIX", wanted
}
So, my question is basically: what is the purpose of ends ?
-j-
--
if you want to contact me by e-mail: j a y a t m i n e d o t b e
.

User: "Ron Natalie"

Title: Re: ostringstream, ends: what am I missing 21 Dec 2003 09:09:35 AM
"jay" <invalid@nowhere.com> wrote in message
news:Xns94588495EABE1jeroenhaegebaerthotm@195.121.6.84...

I was told that an ostringstream should be terminated by ends.

You were told wrong.

So, my question is basically: what is the purpose of ends ?

ends writes a nul byte ('\0') to the stream. Why does it exist and how did
this confusion
come to play? Well heart of the matter is not stringstream, but the
deprecated strstream
classes. These classes, rather than using a std::string to accumulate the
data, use a rather
trickily managed char array. The result, rather than behaving nicely like
a std::string with
a definite length and arbitrary internal contents, fall in to the age-old C
"use a char*" to
fudge a string type. The null byte is required to flag the end of the
C-style string, so putting
it there (either by explicitly writing '\0' or ends) is required. Frankly,
I think it is bogus.
The stream should have placed it itself, just like std::string has to do
when you do c_str()
accesses.
Null bytes are just another character to stringstream and the string class.
If you put one in
the stream or string, it just occupies a position and everything is fine
(until you try to interoperate
with something that cares about nulls, like the old C string functions).
-Ron
.

User: "Andy"

Title: Re: ostringstream, ends: what am I missing 21 Dec 2003 11:05:00 AM
jay <invalid@nowhere.com> wrote in message news:<Xns94588495EABE1jeroenhaegebaerthotm@195.121.6.84>...

I was told that an ostringstream should be terminated by ends.

However, the following code snippet does not do what I would expect if
that were the case:

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
ostringstream oss;
string myString;
long aCounter = 0;

oss << ++aCounter << ends;
myString = oss.str() + "_SUFFIX";

cout << myString << endl; // output: "1 _SUFFIX", unexpected

oss.str("");

oss << ++aCounter;
myString = oss.str() + "_SUFFIX";

cout << myString << endl; // output: "2_SUFFIX", wanted

}

So, my question is basically: what is the purpose of ends ?

-j-

The above code snippet does indeed do what you are expecting it to do.
I ran the code by copy pasting it in my editor. It did not give
"1_SUFFIX" but just "1". Which compiler are you using? I used Borland
8.5 and g++ (not sure which version).
regards,
andy
.
User: "jay"

Title: Re: ostringstream, ends: what am I missing 21 Dec 2003 11:37:48 AM
(Andy) wrote in
news:96dbbc80.0312210905.12b05107@posting.google.com:

jay <invalid@nowhere.com> wrote in message
news:<Xns94588495EABE1jeroenhaegebaerthotm@195.121.6.84>...
The above code snippet does indeed do what you are expecting it to do.
I ran the code by copy pasting it in my editor. It did not give
"1_SUFFIX" but just "1". Which compiler are you using? I used Borland
8.5 and g++ (not sure which version).

OK, so it's now clear to me that the ends thing is not necessary at all,
and I have an explanation for some weird behaviour that I have been
experiencing.
I am using MSVC 7.1. Apparently, putting ends at the end of an
ostringstream results in undefined behaviour, is that correct ?
-j-
.


User: "Rolf Magnus"

Title: Re: ostringstream, ends: what am I missing 21 Dec 2003 06:09:50 AM
jay wrote:

I was told that an ostringstream should be terminated by ends.

Who told you that? Anyway, it's wrong.

So, my question is basically: what is the purpose of ends ?

It writes a '\0' to the stream.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER