469,656 Members | 1,815 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,656 developers. It's quick & easy.

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"
Apr 11 '06 #1
9 8534
李白 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
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
李白 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
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
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
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
李白 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

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
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.

Similar topics

8 posts views Thread by TaiwanNoWhere | last post: by
5 posts views Thread by William Schubert | last post: by
1 post views Thread by KidLogik | last post: by
6 posts views Thread by Christopher Benson-Manica | last post: by
5 posts views Thread by cherico | last post: by
3 posts views Thread by Kyle Kolander | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.