472,995 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,995 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 36317
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer =...
1
by: KidLogik | last post by:
Hello! I am using std::stringstream && std::string to parse a text file -> std::ifstream in; std::string s; std::streamstring ss; int n; I grab each line like so -> std::getline(in,s);
5
by: Marcin Kalicinski | last post by:
Is there a vectorstream class that implements the functionality similar to std::stringstream but with std::vector, not std::string? cheers, Marcin
5
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
1
by: magix | last post by:
I got this reply in my previous post a month ago: May I know, how can I automatically create the folder if it doesn't exist ? In previous reply, it said: -------------------------...
2
by: akitoto | last post by:
Hi there, I am using the std::stringstream to create a byte array in memory. But it is not working properly. Can anyone help me? Code: #include <vector> #include <sstream> #include...
7
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
7
by: Grey Alien | last post by:
Does *ANYONE* in here know how I may parse the various date/time 'elements' from a string?. The input string has the ff format: 'YYYY-MM-DD HH:MM:SS AM'
3
by: Rune Allnor | last post by:
Hi all. I am trying to use std::stringstream to validate input from a file. The strategy is to read a line from the file into a std::string and feed this std::string to an object which breaks it...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.