| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Giampiero Gabbiani" |
| Date: |
19 Oct 2003 12:15:30 PM |
| Object: |
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
.
|
|
| User: "Ron Natalie" |
|
| Title: Re: std:stringstream reset |
19 Oct 2003 12:42:06 PM |
|
|
"Giampiero Gabbiani" <giampiero_gabbiani@tin.it> wrote in message news:pan.2003.10.19.17.15.26.3702@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).
.
|
|
|
| User: "Giampiero Gabbiani" |
|
| Title: Re: std:stringstream reset |
19 Oct 2003 12:49:36 PM |
|
|
Il Sun, 19 Oct 2003 13:42:06 -0400, Ron Natalie ha scritto:
"Giampiero Gabbiani" <giampiero_gabbiani@tin.it> wrote in message news:pan.2003.10.19.17.15.26.3702@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
.
|
|
|
|
|
| User: "WW" |
|
| Title: Re: std:stringstream reset |
19 Oct 2003 12:35:21 PM |
|
|
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
.
|
|
|
| User: "Giampiero Gabbiani" |
|
| Title: Re: std:stringstream reset |
19 Oct 2003 12:40:03 PM |
|
|
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.
.
|
|
|
|
|
| User: "Gianni Mariani" |
|
| Title: Re: std:stringstream reset |
19 Oct 2003 12:40:09 PM |
|
|
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";
}
.
|
|
|
| User: "Giampiero Gabbiani" |
|
| Title: Re: std:stringstream reset |
19 Oct 2003 12:49:22 PM |
|
|
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
.
|
|
|
|
|

|
Related Articles |
|
|