473,662 Members | 2,704 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 2062
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*****@nowher e.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*****@nowher e.com> wrote in message news:<Xn******* *************** ************@19 5.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***********@y ahoo.com (Andy) wrote in
news:96******** *************** ***@posting.goo gle.com:
jay <in*****@nowher e.com> wrote in message
news:<Xn******* *************** ************@19 5.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
6198
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 (prerelease) ===========================================
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 object? Any comment is appreciated.
5
2361
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: std::ostringstream::ostringstream(const std::string&) Gives some weird results on both Solaris & Linux. Either that or I'm missing something. I've made a simple program to
3
5601
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
1591
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
4519
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
1936
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 " << 12.33 << " and a number";
7
2867
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 returned string object still valid after myfunction() has returned? I
11
3637
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 tried a std::ostringstream: std::ostringstream oss; oss << x << y << z; std::string result ( oss.str() ); The result shows that feeding the ostringstream with a string just
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8768
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8547
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7368
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5655
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1754
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.