Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is the deal with 'ostreambuffer'? (how can I empty it?)

1 view
Skip to first unread message

Ramon F Herrera

unread,
Oct 3, 2009, 9:23:33 PM10/3/09
to

Being a newbie, I am by now used to emptying every object like this:

whatever.clear();

It looks like the designers were not consistent, since when applied to
ostringstream the 'clear()' member function sets some flags or
something. I also tried:

myostringstream.flush();

But it is not working, either.

TIA,

-Ramon

Alf P. Steinbach

unread,
Oct 3, 2009, 9:58:27 PM10/3/09
to
* Ramon F Herrera:

Don't reuse a stream.

Just create a new stream object.


Cheers & hth.,

- Alf

Ramon F Herrera

unread,
Oct 3, 2009, 11:53:55 PM10/3/09
to

That's what I reluctantly did - What can I say, I am a newborn
recycler. :^D

-Ramon

Ulrich Eckhardt

unread,
Oct 4, 2009, 3:24:29 AM10/4/09
to
Ramon F Herrera wrote:
> Being a newbie, I am by now used to emptying every object like this:
>
> whatever.clear();

This works with all container classes that came from the STL or are inspired
by it.



> It looks like the designers were not consistent, since when applied to
> ostringstream the 'clear()' member function sets some flags or
> something.

For your info: The design of the STL and the design of the IOStreams took
place separately by independent designers before they were later on
incorporated into the C++ standard library. This explains the stylistic
differences between the two.

> I also tried:
>
> myostringstream.flush();
>
> But it is not working, either.

What exactly do you want? An explicit 'flush()' should empty all the buffers
by actually performing the requested output operations, giving you empty
buffers. Not that there are actually ways for you to detect that, so this
can't be what you want.

Uli

Francis Glassborow

unread,
Oct 4, 2009, 4:54:07 AM10/4/09
to
But he is trying to flush() an ostringstream object. All flush does is
to write out pending material to the destination. When the destination
is a file or the console it makes perfectly good sense. When it is an
ostringstream flush() transfers any pending data to the object's
internal buffer. If the OP wants to reset the internal buffer to empty
he needs to use the appropriate overload of str()

myostringstream.str("");

Ramon F Herrera

unread,
Oct 13, 2009, 10:10:32 AM10/13/09
to
On Oct 4, 4:54 am, Francis Glassborow


I KNEW that there had to be some way to empty things. There always
are. I will try it right away...

My code looks like this:

istringstream str2int(year_str);
int year;
str2int >> year;

Which is rather nice, but it does not scale well (stylistically
speaking):

istringstream str3int(another_str);
int another;
str3int >> another;

istringstream str4int(some_str);
int some;
str4int >> some;

istringstream str5int(whatever_str);
int ;
str5int >> whatever;

-Ramon

Bart van Ingen Schenau

unread,
Oct 13, 2009, 12:49:53 PM10/13/09
to
Ramon F Herrera wrote:

>
> I KNEW that there had to be some way to empty things. There always
> are. I will try it right away...
>
> My code looks like this:
>
> istringstream str2int(year_str);
> int year;
> str2int >> year;
>
> Which is rather nice, but it does not scale well (stylistically
> speaking):
>
> istringstream str3int(another_str);
> int another;
> str3int >> another;
>
> istringstream str4int(some_str);
> int some;
> str4int >> some;
>
> istringstream str5int(whatever_str);
> int ;
> str5int >> whatever;

Duplicating a piece of code never scales well (especially stylistically
speaking).
Once you find that you need the same piece of code in two (or three, or
more) places, it becomes time to move that piece of code to a separate
function:

int str2int(const string& src)
{
int dest;
istringstream iss(src);
iss >> dest;
return dest;
}

int year = str2int(year_str);
int another = str2int(another_str);
int some = str2int(some_str);
int whatever = str2int(whatever_str);

>
> -Ramon

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

Ramon F Herrera

unread,
Oct 13, 2009, 6:58:03 PM10/13/09
to
On Oct 13, 12:49 pm, Bart van Ingen Schenau <b...@ingen.ddns.info>
wrote:


You are absolutely right, Bart.

Thanks!

-Ramon

Jerry Coffin

unread,
Oct 13, 2009, 11:38:00 PM10/13/09
to
In article <67bd1131-d88a-4ed0-8042-ef204d12ca37
@m38g2000yqd.googlegroups.com>, ra...@conexus.net says...

[ ... ]

> My code looks like this:
>
> istringstream str2int(year_str);
> int year;
> str2int >> year;

What you have is virtually identical to a boost lexical_cast:

int year = lexical_cast<int>(year_str);

Another possibility looks something like this:

template <class T>
void convert(std::string const &input, T &result) {
std::istringstream cvt(input);
cvt>>result;
}

which you'd use something like:

int year;
convert(year_str, year);

As it stands at the moment, the second provides no real advantage --
but if you want to assure that (for example) the _entire_ input
string converted to the desired result type, you can add that pretty
easily:

template <class T>
bool convert(std::string const &input, T &result) {
std::istringstream cvt(input);
cvt>>result;
char ch;
return !(cvt>>ch);
}

if (!convert(str_year, year))
// str_year didn't (entirely) convert

--
Later,
Jerry.

0 new messages