By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

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

P: n/a
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"
Apr 11 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
李白 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"


Apr 11 '06 #2

P: n/a
this is not a complete method. need a operate like a basic_stirng::erase().
not only stringstream, also fstream etc.
"Rolf Magnus" <ra******@t-online.de> 写入消息新闻:e1*************@news.t-online.com...
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"

Apr 11 '06 #3

P: n/a
李白 wrote:
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?

Apr 11 '06 #4

P: n/a
a function can clear all stream of the basic_ios object , fstream,
stringstream, wstringstream...
"Rolf Magnus" <ra******@t-online.de> 写入消息新闻:e1*************@news.t-online.com...
wrote:
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?

Apr 11 '06 #5

P: n/a
a algrithom in stl, the basic_ios not iterator
"Rolf Magnus" <ra******@t-online.de> 写入消息新闻:e1*************@news.t-online.com...
wrote:
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?

Apr 11 '06 #6

P: n/a
In article <e1**********@news.cn99.com>, "李白" <zh***@163.com> 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<<"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?
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 11 '06 #7

P: n/a
李白 wrote:
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
Apr 11 '06 #8

P: n/a

Daniel T. wrote in message ...
In article <e1**********@news.cn99.com>, "李白" <zh***@163.com> 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<<"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?

Add:
OP("李白"), try this:

// #includes here
int main(){
std::ostringstream 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?

--
Bob R
POVrookie
Apr 12 '06 #9

P: n/a
In article <pQ*****************@bgtnsc04-news.ops.worldnet.att.net>,
"BobR" <Re***********@worldnet.att.net> wrote:
Daniel T. wrote in message ...
In article <e1**********@news.cn99.com>, "李白" <zh***@163.com> 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<<"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?

Add:
OP("李白"), try this:

// #includes here
int main(){
std::ostringstream 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.
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 12 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.