Clear a stream

Z

Zachary Turner

How do I flush a strstream buffer so that the next time I call .str() I
get back an empty string?

Thanks
 
W

wittempj

Just put an empty string in it.

#include <cstdlib>
#include <sstream>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
stringstream s;

s << "Blah";
cout << "1: " << s.str() << "." << endl;

s.str("");
cout << "2: " << s.str() << "." << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
 
V

Victor Bazarov

Zachary said:
How do I flush a strstream buffer so that the next time I call .str() I
get back an empty string?

Do you really need to use the old char* based streams? In a stringstream,
(std::string-based stream) you simply do

ss.str(""); ss.clear();

and it's fine. The old (strstream) most likely won't work like that.

V
 
Z

Zachary Turner

Yeah I actually meant stringstream. I don't use strstream. Anyway
funny that the solution is so trivial. I looked in MSDN help for ages
(using Visual C++) and it honest to god just doesn't mention that
overload of the str() function. And I type so fast I've just never
noticed the intellisense pop up when i type the ( to see that it has an
overloaded version. I've been wondering about that question for longer
than I care to admit but it never affected me to the point I had to ask
on a forum. Now I feel dumb, hehe.
 
M

Mike Wahler

Zachary Turner said:
Yeah I actually meant stringstream. I don't use strstream. Anyway
funny that the solution is so trivial. I looked in MSDN help for ages
(using Visual C++) and it honest to god just doesn't mention that
overload of the str() function.

Really? From the help file with my VC++v6.0:

basic_stringstream
template <class E,
class T = char_traits<E>,
class A = allocator<E> >
class basic_stringstream : public basic_iostream<E, T> {
public:
explicit basic_stringstream(ios_base::eek:penmode mode = ios_base::in |
ios_base::eek:ut);
explicit basic_stringstream(const basic_string<E, T, A>& x,
ios_base::eek:penmode mode = ios_base::in | ios_base::eek:ut);
basic_stringbuf<E, T, A> *rdbuf() const;
basic_string<E, T, A>& str();
void str(const basic_string<E, T, A>& x);
};


Looks like two overloads to me. :)

-Mike
 
V

Victor Bazarov

Mike Wahler said:
[...]
basic_string<E, T, A>& str();
void str(const basic_string<E, T, A>& x);

Looks like two overloads to me. :)

Are you kiddin'? One name is written aaaall the way to the right
and the other - aaaaalll the way to the left. Overload my foot!...
 
H

Howard

Victor Bazarov said:
Mike Wahler said:
[...]
basic_string<E, T, A>& str();
void str(const basic_string<E, T, A>& x);

Looks like two overloads to me. :)

Are you kiddin'? One name is written aaaall the way to the right
and the other - aaaaalll the way to the left. Overload my foot!...
:)
Hey, don't laugh! I must have stared at that code for 2-3 minutes before I
spotted that first overload. Which is why, I think, my boss always formats
his code so that the identifiers (i.e., "str" in this case) line up in a
single column. It's too weird for me, but at least he can find the member
names quickly.
-Howard
 
M

Mike Wahler

Howard said:
Victor Bazarov said:
Mike Wahler said:
[...]
basic_string<E, T, A>& str();
void str(const basic_string<E, T, A>& x);

Looks like two overloads to me. :)

Are you kiddin'? One name is written aaaall the way to the right
and the other - aaaaalll the way to the left. Overload my foot!...
:)
Hey, don't laugh! I must have stared at that code for 2-3 minutes before I
spotted that first overload. Which is why, I think, my boss always formats
his code so that the identifiers (i.e., "str" in this case) line up in a
single column. It's too weird for me, but at least he can find the member
names quickly.

Long, long ago, in a galaxy far far away, I was told that
essential to programming is attention to detail. I've
never forgotten that. :)

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,298
Messages
2,571,540
Members
48,275
Latest member
tetedenuit01

Latest Threads

Top