Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "clear" an ostringstream

Reply
Thread Tools

"clear" an ostringstream

 
 
wang
Guest
Posts: n/a
 
      08-17-2010
On 16 Aug., 17:09, wang <king_ww...@yahoo.de> wrote:
> On 15 Aug., 22:23, "Francesco S. Carta" <entul...@gmail.com> wrote:
>
>
>
>
>
> > > #include<string>
> > > #include<sstream>
> > > #include<iostream>
> > > using namespace std;
> > > int main() {
> > > * *double x = 1.2, y = 6.5;
> > > * *string xs, ys;
> > > * *ostringstream oss;
> > > * *oss<< *x;
> > > * *xs = oss.str();
> > > * *oss.str(string());
> > > * *oss<< *y;
> > > * *ys = oss.str();
> > > * *cout<< *xs<< *endl;
> > > * *cout<< *ys<< *endl;
> > > * *return 0;
> > > }

>
> > Just for the records, notice that your original solution would have
> > worked fine - quoting your words from the original post:

>
> > > I inserted "oss.str("");" before "oss << y;",

>
> > oss.str("");

>
> > and

>
> > oss.str(string());

>
> > achieve just the same result.

>
> > Hence there must have been some other problem hanging around when you
> > posted your original question.

>
> I'm to 99% certain that you are right. Neither oss.str("") nor
> oss.str(string()) works with MS VC++6. On Ubuntu Linux with g++,
> oss.str(string()) works just fine. I've forgotten if I've tested with
> oss.str("") on Linux.
> Since I realized that VC++6 doesn't work with ostringstream this way,
> I went back to ANSI C and used sprintf() to fulfil my task. I wanted
> to write "pure" C++ code, but VC++6 doesn't let me do it.
> kwwang- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -


On Ubuntu Linux with g++, oss.str("") works fine, too.
kwwang
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      08-17-2010
On Aug 14, 11:50 pm, wang <king_ww...@yahoo.de> wrote:

> I tried to format several numbers into strings using an ostringstream
> in the following way:


> ostringstream oss;
> double x = 1.2, y = 5.3;
> string xs, ys;
> oss << x;
> xs = oss.str();
> oss << y;
> ys = oss.str();


> But ys has the result "1.25.3". Obviously y is appended to x.
> oss must be "cleared" before y is formatted. But how?
> I inserted "oss.str("");" before "oss << y;", the result is
> the same -- the empty string is appended to oss again.


As others have pointed out, inserting the oss.str("") should
work. Still, I'm surprised that no one pointed out the obvious
(and cleanest) solution: just use a new ostringstream each time.
The surest way of getting a "cleared" object (of any type) is to
construct a new one. `oss.str("")' only clears the string's
contents, for example; it doesn't reset any formatting flags or
whatever that might have been set.

--
James Kanze
 
Reply With Quote
 
 
 
 
wang
Guest
Posts: n/a
 
      08-18-2010
On 17 Aug., 12:20, James Kanze <james.ka...@gmail.com> wrote:
> On Aug 14, 11:50 pm, wang <king_ww...@yahoo.de> wrote:
>
> > I tried to format several numbers into strings using an ostringstream
> > in the following way:
> > ostringstream oss;
> > double x = 1.2, y = 5.3;
> > string xs, ys;
> > oss << x;
> > xs = oss.str();
> > oss << y;
> > ys = oss.str();
> > But ys has the result "1.25.3". Obviously y is appended to x.
> > oss must be "cleared" before y is formatted. But how?
> > I inserted "oss.str("");" before "oss << y;", the result is
> > the same -- the empty string is appended to oss again.

>
> As others have pointed out, inserting the oss.str("") should
> work. Still, I'm surprised that no one pointed out the obvious
> (and cleanest) solution: just use a new ostringstream each time.
> The surest way of getting a "cleared" object (of any type) is to
> construct a new one. *`oss.str("")' only clears the string's
> contents, for example; it doesn't reset any formatting flags or
> whatever that might have been set.
>
> --
> James Kanze


Yes, I came to this idea, too. I wrote a subprogramm (or a private
method?) to do the task, and the ostringstream is defined in this
subprogramm. This way I avoided using sprintf().
kwwang
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: using ZLIB to compress std::ostringstream? Mike C++ 1 08-03-2003 06:50 PM
Re: ? How to clear ostringstream buffer Mark Warren C++ 3 07-23-2003 10:12 AM
vector of ostringstream Alex Vinokur C++ 4 07-14-2003 02:36 AM
Re: std::ostringstream unexpected behavior with .net 2003. Russell Hanneken C++ 0 06-25-2003 10:22 PM
Re: std::ostringstream unexpected behavior with .net 2003. Victor Bazarov C++ 0 06-25-2003 10:20 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57