how to clear a stringstream object's data, not state.

À

Àî°×

hi all:

I want erase a stringstream' content for input new data to it.

example:
std::stringstream stm;
stm<<"this is a string";

std::cout<<stm.str();
// here print:this is a string

// do something here of the stm, i don't know .....
stm<<"not a string";

std::cout<<strm.str();
// I want print "not a string", not "this is a string not a string"
 
R

Rolf Magnus

Àî°× said:
hi all:

I want erase a stringstream' content for input new data to it.

example:
std::stringstream stm;
stm<<"this is a string";

std::cout<<stm.str();
// here print:this is a string

// do something here of the stm, i don't know .....

stm.str("");
 
À

Àî°×

this is not a complete method. need a operate like a basic_stirng::erase().
not only stringstream, also fstream etc.
 
R

Rolf Magnus

Àî°× said:
this is not a complete method.

It does exactly what you asked for. It clears a stringstream's content.
need a operate like a basic_stirng::erase(). not only stringstream, also
fstream etc.

Well, you didn't mention fstream until now, so how could I have known this?
Anyway, what do you want it to do for an fstream? Remove the file? Replace
it with an empty file?
 
À

Àî°×

a function can clear all stream of the basic_ios object , fstream,
stringstream, wstringstream...
 
À

Àî°×

a algrithom in stl, the basic_ios not iterator
Rolf Magnus said:
wrote:


It does exactly what you asked for. It clears a stringstream's content.


Well, you didn't mention fstream until now, so how could I have known
this?
Anyway, what do you want it to do for an fstream? Remove the file? Replace
it with an empty file?
 
D

Daniel T.

hi all:

I want erase a stringstream' content for input new data to it.

example:
std::stringstream stm;
stm<<"this is a string";

std::cout<<stm.str();
// here print:this is a string

// do something here of the stm, i don't know .....
stm<<"not a string";

std::cout<<strm.str();
// I want print "not a string", not "this is a string not a string"
need a operate like a basic_stirng::erase(). not only stringstream,
also fstream etc.
a algrithom in stl, the basic_ios not iterator
a function can clear all stream of the basic_ios object , fstream,
stringstream, wstringstream...

You still haven't made it clear what you want. basic_ios objects don't
have a str() function so they wouldn't work in the sample code you
provided.

To clear an istream, use "ignore()".

What would it mean to clear an ostream? How would you know it's cleared?
 
T

Tom Widmer

Àî°× said:
a function can clear all stream of the basic_ios object , fstream,
stringstream, wstringstream...

What do you mean by "clear"? What would it mean to clear a printer? In
general, streams cannot be cleared.

Tom
 
B

BobR

Daniel T. wrote in message ...
You still haven't made it clear what you want. basic_ios objects don't
have a str() function so they wouldn't work in the sample code you
provided.

To clear an istream, use "ignore()".

What would it mean to clear an ostream? How would you know it's cleared?


Add:
OP("Àî°×"), try this:

// #includes here
int main(){
std::eek:stringstream Oss; // note type of stream.

Oss<<"this is a string";
std::cout<<Oss.str(); // here print: 'this is a string'

Oss.str(""); // empty the ostringstream object buffer.
// Oss.clear(); // if stream was in fail state.

// do something here of the stm, i don't know .....
Oss<<"not a string";
std::cout<<Oss.str();

return 0;
} // main() end

Is that what you ("Àî°×") were looking for?
 
D

Daniel T.

"BobR said:
Daniel T. wrote in message ...


Add:
OP("Àî°×"), try this:

// #includes here
int main(){
std::eek:stringstream Oss; // note type of stream.

Oss<<"this is a string";
std::cout<<Oss.str(); // here print: 'this is a string'

Oss.str(""); // empty the ostringstream object buffer.
// Oss.clear(); // if stream was in fail state.

// do something here of the stm, i don't know .....
Oss<<"not a string";
std::cout<<Oss.str();

return 0;
} // main() end

Is that what you ("Àî°×") were looking for?

He's already said he wants something that will work with any stream, not
just stringstreams.
 
Joined
Jul 1, 2011
Messages
1
Reaction score
0
works nicely

Àî°× wrote:

> hi all:
>
> I want erase a stringstream' content for input new data to it.
>
> example:
> std::stringstream stm;
> stm<<"this is a string";
>
> std::cout<<stm.str();
> // here print:this is a string
>
> // do something here of the stm, i don't know .....


stm.str("");

> stm<<"not a string";
>
> std::cout<<strm.str();
> // I want print "not a string", not "this is a string not a string"

Perfect! Exactly what I needed! Thanks Rolf!
 

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

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top