471,585 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

std:stringstream reset

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
6 35860
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
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
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

"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
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
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.

Similar topics

2 posts views Thread by Woodster | last post: by
1 post views Thread by KidLogik | last post: by
5 posts views Thread by Marcin Kalicinski | last post: by
1 post views Thread by magix | last post: by
2 posts views Thread by akitoto | last post: by
7 posts views Thread by Grey Alien | last post: by
3 posts views Thread by Rune Allnor | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.