By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,553 Members | 1,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,553 IT Pros & Developers. It's quick & easy.

std:stringstream reset

P: n/a
Is it possible to reset a std::stringstream in order to reuse it once more?
I tried with flush() method without any success...
Thanks in advance
Giampiero

Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
WW
Giampiero Gabbiani wrote:
Is it possible to reset a std::stringstream in order to reuse it once
more? I tried with flush() method without any success...


Have you tried seeking to the beinning?

--
WW aka Attila
Jul 19 '05 #2

P: n/a
Il Sun, 19 Oct 2003 20:35:21 +0300, WW ha scritto:
Giampiero Gabbiani wrote:
Is it possible to reset a std::stringstream in order to reuse it once
more? I tried with flush() method without any success...


Have you tried seeking to the beinning?

Yes, I tried seekp(0), but it continues not to reset the stream.
Actually I use this stream (a std::ostringstream) for writing message to
system log, so I use the str() method in order to print the message in the stream.

Jul 19 '05 #3

P: n/a
Giampiero Gabbiani wrote:
Is it possible to reset a std::stringstream in order to reuse it once more?
I tried with flush() method without any success...
Thanks in advance
Giampiero

The "str()" and "str( string )" methods should work.
However, on some platforms these may not work so you
may use the ugly reconstruct hack.
#include <sstream>
#include <iostream>

int main()
{

std::istringstream iss( "Hi there" );

std::ostringstream oss;

std::string foo;

iss >> foo;
oss << foo;

iss.str( "boo hoo" ); // Set the input to new string

iss >> foo;
oss << foo;
std::cout << oss.str() << "\n";

// Another way is to reconstruct the istringstream
// UGLY but some implementations have bugs and this is the
// only way.
iss.~istringstream();
new ( (void *) &iss ) std::istringstream( "Reconstruct this" );

oss.str( "" ); // reset the output string.

iss >> foo;
oss << foo;
std::cout << oss.str() << "\n";

// Another way is to reconstruct the ostringstream
oss.~ostringstream();
new ( (void *) &oss ) std::ostringstream;

iss >> foo;
oss << foo;
std::cout << oss.str() << "\n";

}

Jul 19 '05 #4

P: n/a

"Giampiero Gabbiani" <gi****************@tin.it> wrote in message news:pa**************************@tin.it...
Is it possible to reset a std::stringstream in order to reuse it once more?
I tried with flush() method without any success...


Just set the string via the str(const string&) method (to either an empty string or
one with new data as appropriate for what you are doing).
Jul 19 '05 #5

P: n/a
Il Sun, 19 Oct 2003 17:40:09 +0000, Gianni Mariani ha scritto:
Giampiero Gabbiani wrote:
Is it possible to reset a std::stringstream in order to reuse it once more?
I tried with flush() method without any success...
Thanks in advance
Giampiero

The "str()" and "str( string )" methods should work.
However, on some platforms these may not work so you
may use the ugly reconstruct hack.
#include <sstream>
#include <iostream>

int main()
{

std::istringstream iss( "Hi there" );

std::ostringstream oss;

std::string foo;

iss >> foo;
oss << foo;

iss.str( "boo hoo" ); // Set the input to new string

iss >> foo;
oss << foo;
std::cout << oss.str() << "\n";

// Another way is to reconstruct the istringstream
// UGLY but some implementations have bugs and this is the
// only way.
iss.~istringstream();
new ( (void *) &iss ) std::istringstream( "Reconstruct this" );

oss.str( "" ); // reset the output string.

iss >> foo;
oss << foo;
std::cout << oss.str() << "\n";

// Another way is to reconstruct the ostringstream
oss.~ostringstream();
new ( (void *) &oss ) std::ostringstream;

iss >> foo;
oss << foo;
std::cout << oss.str() << "\n";

}

Thanks a lot, I succeeded in resetting the stream with str("") method
call!
Bye
Giampiero
Jul 19 '05 #6

P: n/a
Il Sun, 19 Oct 2003 13:42:06 -0400, Ron Natalie ha scritto:

"Giampiero Gabbiani" <gi****************@tin.it> wrote in message news:pa**************************@tin.it...
Is it possible to reset a std::stringstream in order to reuse it once more?
I tried with flush() method without any success...


Just set the string via the str(const string&) method (to either an empty string or
one with new data as appropriate for what you are doing).

Thanks a lot, I succeeded in resetting the stream with str("") method
call!
Bye
Giampiero
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.