std:stringstream reset

G

Giampiero Gabbiani

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
 
W

WW

Giampiero said:
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?
 
G

Giampiero Gabbiani

Il Sun, 19 Oct 2003 20:35:21 +0300, WW ha scritto:
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::eek:stringstream) for writing message to
system log, so I use the str() method in order to print the message in the stream.
 
G

Gianni Mariani

Giampiero said:
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::eek:stringstream 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::eek:stringstream;

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

}
 
R

Ron Natalie

Giampiero Gabbiani said:
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).
 
G

Giampiero Gabbiani

Il Sun, 19 Oct 2003 17:40:09 +0000, Gianni Mariani ha scritto:
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::eek:stringstream 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::eek:stringstream;

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

}
Thanks a lot, I succeeded in resetting the stream with str("") method
call!
Bye
Giampiero
 
G

Giampiero Gabbiani

Il Sun, 19 Oct 2003 13:42:06 -0400, Ron Natalie ha scritto:
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
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top