Possible Duplicate:
How to clear stringstream?
I have this c++ command
//siteurl and filename are of string type
stringstream ss;
ss << "lynx -dump '" << siteurl << "' > " << filename;
On first loop, everything ok. But on second loop i realize it just append instead of create a new record
as i troubleshoot using cout it give me this, I realize the command is append instead of overwrite on 2nd loop, how do I null or reset or overwrite first record of ss (stringstream) on 2nd loop/usage.
Webparser: lynx -dump 'http://sg.finance.yahoo.com/q?s=USDSGD=X' > file.txtlynx -dump 'http://sg.finance.yahoo.com/q?s=EURUSD=X' > file.txt
What i want is on 2nd loop it will be this output
Webparser: lynx -dump 'http://sg.finance.yahoo.com/q?s=EURUSD=X' > file.txt
I am new to C++, thanks for all help, greatly apperciated.
Do
ss.str("")to make it empty.But it is better to define
ssinside the loop, so that each time it’s a new variable:That is, make it a scope variable, e.g function-scope or block-scope, so that when it goes out of scope it is destructed, and when it comes back, it gets created again.
Making it a scope variable is less error prone than using
ss.str("")to make it empty. I mean, what if you forgot to doss.str("")? In many cases, when you need to writess.str("")indicates that you actually need to make it a scope variable, so that you could define it again, either in a loop (as shown above), or entirely a new variable, probably with a different name.