473,769 Members | 6,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::ostringstr eam bizarre behaviour. Please help!

I am having some strange problems using std::ostringstr eam.

The simple stuff works okay, but trying to use:
std::ostringstr eam::str(const std::string&)
or:
std::ostringstr eam::ostringstr eam(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
demonstrate the problem:

-------------cut-as-ostringstreambu g.cc---------------------------
#include <string>
#include <iostream>
#include <sstream>
int main(void)
{
int rc = 0;
std::ostringstr eam oss1;
oss1 << "foo";
oss1.str(std::s tring());
if (oss1.str() != std::string("") )
{ ++rc; std::cout << __LINE__ << ": Got \""
<< oss1.str() << '\"' << std::endl; }
std::ostringstr eam oss2(std::strin g("foo"));
oss2 << "bar";
if (oss2.str() != std::string("fo obar"))
{ ++rc; std::cout << __LINE__ << ": Got \""
<< oss2.str() << '\"' << std::endl; }
std::ostringstr eam oss3;
oss3 << "foo";
oss3.str(std::s tring("bar"));
oss3 << "foo";
if (oss3.str() != std::string("ba rfoo"))
{ ++rc; std::cout << __LINE__ << ": Got \""
<< oss3.str() << '\"' << std::endl; }
return rc;
}
--------------------cut-end----------------------------------------

On Solaris:
CC -V
CC: WorkShop Compilers 5.0 98/12/15 C++ 5.0
CC -o ostringstreambu g ostringstreambu g.cc
ostringstreambu g;echo $?
11: Got ""
16: Got "bar"
23: Got "foo"
3

On Linux:
g++ -v
gcc version 3.2.2
g++ -o ostringstreambu g ostringstreambu g.cc
ostringstreambu g;echo $?
16: Got "bar"
23: Got "foo"
2

Now, I would expect to see no output and a return value of 0 on all
machines.

The only thing I can think of is that there is some sort of "freezing" the
way there was with the old "char* ostrstream::str ()" stuff, but I don't
find anything that documents the intended behaviour.

Can anyone help?????????? please????

--

+------------------------------------------------------------------
| Simon Pryor, mailto:si****** *********@alcat el.be
| Alcatel Bell Space N.V.
| Berkenrodelei 33, phone: +32 3 829 5130
| B-2660 Hoboken, Belgium. fax: +32 3 829 5502
+------------------------------------------------------------------
Jul 22 '05 #1
5 2369
Simon Pryor wrote:
#include <string>
#include <iostream>
#include <sstream>
int main(void)
{
int rc = 0;
std::ostringstr eam oss1;
oss1 << "foo";
oss1.str(std::s tring());
if (oss1.str() != std::string("") )
{ ++rc; std::cout << __LINE__ << ": Got \""
<< oss1.str() << '\"' << std::endl; }
std::ostringstr eam oss2(std::strin g("foo"));
Try this:

std::ostringstr eam oss2(std::strin g("foo"), std::ios_base:: out |
std::ios_base:: app);
oss2 << "bar";
if (oss2.str() != std::string("fo obar"))
{ ++rc; std::cout << __LINE__ << ": Got \""
<< oss2.str() << '\"' << std::endl; }
std::ostringstr eam oss3;
Try:

std::ostringstr eam oss3(std::ios_b ase::out | std::ios_base:: app);

oss3 << "foo";
oss3.str(std::s tring("bar"));
oss3 << "foo";
if (oss3.str() != std::string("ba rfoo"))
{ ++rc; std::cout << __LINE__ << ": Got \""
<< oss3.str() << '\"' << std::endl; }
return rc;
}
--------------------cut-end----------------------------------------

On Solaris:
CC -V
CC: WorkShop Compilers 5.0 98/12/15 C++ 5.0
CC -o ostringstreambu g ostringstreambu g.cc
ostringstreambu g;echo $?
11: Got ""
16: Got "bar"
23: Got "foo"
3

On Linux:
g++ -v
gcc version 3.2.2
g++ -o ostringstreambu g ostringstreambu g.cc
ostringstreambu g;echo $?
16: Got "bar"
23: Got "foo"
2

Now, I would expect to see no output and a return value of 0 on all
machines.

The only thing I can think of is that there is some sort of "freezing" the
way there was with the old "char* ostrstream::str ()" stuff, but I don't
find anything that documents the intended behaviour.

Can anyone help?????????? please????


According to

http://www.roguewave.com/support/doc...uf.html#idx403

, you need to use out | app for the rdbuf. Otherwise the output sequence
points to the first character in the buffer.

I tried this on gcc version 3.3.1 and it worked. However, I also tried
with Microsoft's C++ compiler version 13.10.3052 and it didn't work
(maybe I should update it :-) ).

HTH.

BR,
Peter
Jul 22 '05 #2
Thanks for your help!

This fully fixed it for Linux and partially fixed it for Solaris.

However, when resetting to a NULL string, on Solaris, there was still a
problem (If you refer to my original posting, you can see there was already
a difference in output between Solaris & Linux.
Running Purify on Solaris also gave all sorts of FMR & UMR errors, with this:

std::ostringstr eam oss1(std::ios_b ase::out | std::ios_base:: app);
oss1 << "foo";
oss1.str(std::s tring()); // or oss1.str("");
if (oss1.str().siz e())
{ ++rc; std::cout << __LINE__ << ": Got "
<< oss1.str().size () << "/" << int(oss1.str(). data()[0])
<< std::endl; }

On Solaris, I get:

11: Got 1/0
1

So there is an internal bug in the Solaris STD library of C++ 5.0!

Maybe a workaround is always to "reset" to a space or newline or
something like that, though that's not always possible to guarantee
everyone doing this :-(

--

+------------------------------------------------------------------
| Simon Pryor, mailto:si****** *********@alcat el.be
| Alcatel Bell Space N.V.
| Berkenrodelei 33, phone: +32 3 829 5130
| B-2660 Hoboken, Belgium. fax: +32 3 829 5502
+------------------------------------------------------------------
Jul 22 '05 #3
Peter Kragh wrote:
According to

http://www.roguewave.com/support/doc...uf.html#idx403

, you need to use out | app for the rdbuf. Otherwise the output sequence
points to the first character in the buffer.

I tried this on gcc version 3.3.1 and it worked. However, I also tried
with Microsoft's C++ compiler version 13.10.3052 and it didn't work
(maybe I should update it :-) ).

It didn't work on Microsoft C++ compiler version 13.10.3077 and
Borland C++ compiler version 5.6.4 too. (Dinkumware & STLPort IIRC)

Jul 22 '05 #4
> Try this:

std::ostringstr eam oss2(std::strin g("foo"), std::ios_base:: out |
std::ios_base:: app);


Unfortunately there is nothing in the standard which indicates ostringstream
takes any notice of flags like std::ios_base:: app. So implementations are
free to ignore them in constructors

Stephen

Jul 22 '05 #5
Stephen Howe wrote:
Unfortunately there is nothing in the standard which indicates ostringstream
takes any notice of flags like std::ios_base:: app. So implementations are
free to ignore them in constructors


Maybe, I misunderstood chapter 27.7.3.1 (which I should have refered to
in the first place) then:

<quote>
Constructs an object of class basic_ostringst ream, initializing the base
class with basic_ostream(& sb) and initializing sb with
basic_stringbuf <charT,traits,A llocator>( which | ios_base::out))
</quote>

Please correct me, if I'm wrong.

- Peter
Jul 22 '05 #6

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

Similar topics

6
3026
by: Eric Boutin | last post by:
Hi ! I have a strange problem with a std::ostringstream.. code : #include <sstream> /*...*/ std::ostringstream ss(); ss << "\"\"" << libpath << "\"\" \"\"" << argfilename << "\"\" \"\"" << outfilename << "\"\""; //line 75
3
2805
by: Chris | last post by:
Hi, I'm playing/developing a simple p2p file sharing client for an existing protocol. A 2 second description of the protocol is Message Length = int, 4 bytes Message Code = int, 4 bytes Data
6
4847
by: Christopher Benson-Manica | last post by:
Is there anything that one can do to a std::ostringstream that would make its destructor explode? I'm basically doing { std::ostringstream ss, msg; // do BUNCHES of stuff with ss and msg // use ss.str() and msg.str(), apparently successfully
1
2380
by: Jason Heyes | last post by:
I would like to have std::cout redirect its output to a std::ostringstream. Can this be done? Help is appreciated.
1
2209
by: Voronkov Konstantin | last post by:
Hello all! std::ostringstream stream; stream << 8080; std::string str = stream.str(); // str == "8 080" The code shown above in mine big program result the str variable value to
2
3553
by: bob | last post by:
For reasons that are irrelevant , we cannot use sprintf to do conversions between doubles, ints etc. to char*. We're using the std::ostringstream type. Basically we have a function that takes a double and needs to store a string representation of it into a vector of strings. ... something like this; void pussy::(const double d) { std::ostringstream strm;
4
4526
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?
25
8376
by: Bala2508 | last post by:
Hi, I have a C++ application that extensively uses std::string and std::ostringstream in somewhat similar manner as below std::string msgHeader; msgHeader = "<"; msgHeader += a; msgHeader += "><";
2
5309
by: mathieu | last post by:
Hi, I am playing with the following C++ piece of code (*). At least on my system debian/gcc 4.3 it looks like I am not writing out a floating point separator as a comma. what are the operation affected by the LC_NUMERIC env var value ? Thanks -Mathieu
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10049
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
9997
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
9865
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
8873
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...
0
6675
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();...
0
5309
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.