473,508 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ostringstream, ends: what am I missing

jay
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
Jul 22 '05 #1
4 2052
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.

Jul 22 '05 #2

"jay" <in*****@nowhere.com> wrote in message
news:Xn**********************************@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
Jul 22 '05 #3
jay <in*****@nowhere.com> wrote in message news:<Xn**********************************@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
Jul 22 '05 #4
jay
ga***********@yahoo.com (Andy) wrote in
news:96**************************@posting.google.c om:
jay <in*****@nowhere.com> wrote in message
news:<Xn**********************************@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-
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6188
by: Alex Vinokur | last post by:
Is it possible to use vector<ostringstream> ? Here is what I have got. =========================================== Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927...
5
743
by: Als | last post by:
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...
5
2346
by: Simon Pryor | last post by:
I am having some strange problems using std::ostringstream. The simple stuff works okay, but trying to use: std::ostringstream::str(const std::string&) or:...
3
5587
by: Mathieu Malaterre | last post by:
Hello, I am trying to write this simple code: std::ostringstream s; s << 1024; std::cout << s.str() << std::endl; s.str(""); // <- problem s << 512; std::cout << s.str() << std::endl;
1
1587
by: Maitre Bart | last post by:
The following simple program overwrites the content of an ostriungstream object. #include <iostream> #include <sstream> using namespace std; int main() {
4
4500
by: Olaf van der Spek | last post by:
Hi, I'm using ostringstream and I'd like to generate \r\n line ends instead of \n ones. Is there a flag for this? Or should I just insert the \r myself?
15
1914
by: iu2 | last post by:
Hi all, can someone help me with this? I'm trying to shorthen some logging function in our project, i.e., instead of LogString("...); use a more convenient log() << "String here " <<...
7
2860
by: Thomas Lenz | last post by:
Please consider the following code snippet: string myfunction() { ostringstream oss; oss << "junk"; // do something more with oss; I can't make it const... return oss.str(); } Is the...
11
3613
by: coomberjones | last post by:
I have a few std::strings that I am using to store raw binary data, each of which may very well include null bytes at any point or points. I want to slap them together into a single string, so I...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.