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
demonstrate the problem:
-------------cut-as-ostringstreambug.cc---------------------------
#include <string>
#include <iostream>
#include <sstream>
int main(void)
{
int rc = 0;
std::ostringstream oss1;
oss1 << "foo";
oss1.str(std::string());
if (oss1.str() != std::string(""))
{ ++rc; std::cout << __LINE__ << ": Got \""
<< oss1.str() << '\"' << std::endl; }
std::ostringstream oss2(std::string("foo"));
oss2 << "bar";
if (oss2.str() != std::string("foobar"))
{ ++rc; std::cout << __LINE__ << ": Got \""
<< oss2.str() << '\"' << std::endl; }
std::ostringstream oss3;
oss3 << "foo";
oss3.str(std::string("bar"));
oss3 << "foo";
if (oss3.str() != std::string("barfoo"))
{ ++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 ostringstreambug ostringstreambug.cc
ostringstreambug;echo $?
11: Got ""
16: Got "bar"
23: Got "foo"
3
On Linux:
g++ -v
gcc version 3.2.2
g++ -o ostringstreambug ostringstreambug.cc
ostringstreambug;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***************@alcatel.be
| Alcatel Bell Space N.V.
| Berkenrodelei 33, phone: +32 3 829 5130
| B-2660 Hoboken, Belgium. fax: +32 3 829 5502
+------------------------------------------------------------------ 5 2333
Simon Pryor wrote: #include <string> #include <iostream> #include <sstream> int main(void) { int rc = 0; std::ostringstream oss1; oss1 << "foo"; oss1.str(std::string()); if (oss1.str() != std::string("")) { ++rc; std::cout << __LINE__ << ": Got \"" << oss1.str() << '\"' << std::endl; } std::ostringstream oss2(std::string("foo"));
Try this:
std::ostringstream oss2(std::string("foo"), std::ios_base::out |
std::ios_base::app);
oss2 << "bar"; if (oss2.str() != std::string("foobar")) { ++rc; std::cout << __LINE__ << ": Got \"" << oss2.str() << '\"' << std::endl; } std::ostringstream oss3;
Try:
std::ostringstream oss3(std::ios_base::out | std::ios_base::app);
oss3 << "foo"; oss3.str(std::string("bar")); oss3 << "foo"; if (oss3.str() != std::string("barfoo")) { ++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 ostringstreambug ostringstreambug.cc ostringstreambug;echo $? 11: Got "" 16: Got "bar" 23: Got "foo" 3
On Linux: g++ -v gcc version 3.2.2 g++ -o ostringstreambug ostringstreambug.cc ostringstreambug;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
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::ostringstream oss1(std::ios_base::out | std::ios_base::app);
oss1 << "foo";
oss1.str(std::string()); // or oss1.str("");
if (oss1.str().size())
{ ++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***************@alcatel.be
| Alcatel Bell Space N.V.
| Berkenrodelei 33, phone: +32 3 829 5130
| B-2660 Hoboken, Belgium. fax: +32 3 829 5502
+------------------------------------------------------------------
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)
> Try this: std::ostringstream oss2(std::string("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
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_ostringstream, initializing the base
class with basic_ostream(& sb) and initializing sb with
basic_stringbuf<charT,traits,Allocator>( which | ios_base::out))
</quote>
Please correct me, if I'm wrong.
- Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 << "\"\"...
|
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...
|
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
...
|
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.
|
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
|
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...
|
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?
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
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...
| |