473,385 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

clearing contents of ostringstream object

Hello,

How do you clear the contents of an ostringstream object? I've included
some example code so you can see what I mean.

t.i.a.
Craig H.
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

int main(int argc, char* argv[])
{
string a("hello");
string b("world");
string s1;
ostringstream oss;

oss << setiosflags(ios::left) << setfill('O') << setw(10) << a << endl;
s1 = oss.str();
cout << s1;

// how do you clear the ostringstream oss?

oss << resetiosflags(ios::left) << setfill('W') << setw(10) << b << endl;
s1 = oss.str();
cout << s1;

system("PAUSE");
return EXIT_SUCCESS;
}

/*
-curent output-
helloOOOOO
helloOOOOO
WWWWWworld

-desired output-
helloOOOOO
WWWWWworld
*/

--
"Communism won't work because people like to own stuff."
Jul 23 '05 #1
4 7404
news.eircom.net wrote:
Hello,

How do you clear the contents of an ostringstream object?
I've included some example code so you can see what I mean.

t.i.a.
Craig H.
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

int main(int argc, char* argv[])
{
string a("hello");
string b("world");
string s1;
ostringstream oss;

oss << setiosflags(ios::left) << setfill('O') << setw(10) << a << endl;
s1 = oss.str();
cout << s1;

// how do you clear the ostringstream oss?
oss.str("");

oss << resetiosflags(ios::left) << setfill('W') << setw(10) << b << endl;
s1 = oss.str();
cout << s1;

system("PAUSE");
return EXIT_SUCCESS;
}

/*
-curent output-
helloOOOOO
helloOOOOO
WWWWWworld

-desired output-
helloOOOOO
WWWWWworld
*/


Jul 23 '05 #2
On Fri, 01 Jul 2005 13:59:24 +0400, Rolf Magnus <ra******@t-online.de>
wrote:

[]
// how do you clear the ostringstream oss?


oss.str("");


Do not forget to reset flags. The complete clear is:

oss.str("");
oss.clear();

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #3
"Maxim Yegorushkin" <fi****************@gmail.com> wrote in message

Do not forget to reset flags. The complete clear is:

oss.str("");
oss.clear();


From looking at sections 27.7.3.2 and 27.7.1.2 of the standard, it appears
str("") does not clear the stream bits. It seems to me that in clearing the
internal buffer, one would intend that the stream bits would also be reset
(in a good state). Is there a reason this is not the defined behavior?
Aside from a slight performance hit, I have not been able to think of a
situation where it would be beneficial to set the internal buffer to an
empty string (in essence resetting it), but leave the state of the stream
unchanged. If performance is the reason, then under what conditions will an
ostringstream enter a "bad" state (needing to be clear()'ed)?

If I'm just doing stuff like this, is there ever a need to clear():

// contrived example
ostringstream oss;
for (int i = 0; i < 50; ++i)
{
oss << "File:" << i;
cout << oss.str() << endl;
oss.str("");
}

Thanks,
Kyle
Jul 23 '05 #4
On Fri, 01 Jul 2005 22:45:54 +0400, Kyle Kolander <kk*******@hotmail.com>
wrote:

[]
If performance is the reason, then under what conditions will an
ostringstream enter a "bad" state (needing to be clear()'ed)?


Consider:

#include <iostream>
#include <sstream>

int main()
{
using namespace std;

stringstream ss;

cout << ss.rdstate() << endl;

ss.str("a");
int a;
ss >> a;

cout << ss.rdstate() << endl;
}

outputs:
0
4

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alex Vinokur | last post by:
Is it possible to use vector<ostringstream> ? Here is what I have got. =========================================== Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927...
5
by: Als | last post by:
What's the meaning of "freeze" as member of ostringstream class? Is it needed always to call freeze() at the disposal of an ostringstream object? Is it also needed before disposing an istringsteam...
5
by: William Payne | last post by:
How do you get rid the contents in a std::stringstream? I'm using one in a for loop to format some status messages which I print to the screen. The stringstream member function clear() only clears...
8
by: Bob Smith | last post by:
I am downloading over http port 80 some contents froma site, but the contents is not properly stored after using ostringstream for temporary storage, and later ostringstream::str() for passing it...
2
by: jhattersley | last post by:
Hi Folks, Wonder if you can help... All I'm trying to do is load a couple of simple HTML pages (in memory) into the WebBrowser control, one after the other. I don't seem to be able to clear...
3
by: Bob Altman | last post by:
Hi all, Why doesn't the following unmanaged C++ code work as expected: string s; ostringstream strm(s); // This stream should store results in s strm << 25; cout << s << endl; // s...
1
by: itsme | last post by:
Hello, I am storing an XmlDocument into the session. Each time I want to store the XmlDocument into session, I should clear the session object and then store the XmlDocument. Can anyone...
15
by: iu2 | last post by:
Hi all, can someone help me with this? I'm trying to shorthen some logging function in our project, i.e., instead of LogString("...); use a more convenient log() << "String here " <<...
13
by: vsuresh.cs | last post by:
Hi, My c++ program looks likes this void api1() { cin << info; ostringstream ostr; ostr << info; api2(ostr.str().c_str()); // Getting string from ostreamstream and getting char* from string
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.