473,804 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(i os::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 7493
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(i os::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*******@hotm ail.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
6211
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 (prerelease) ===========================================
5
743
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 object? Any comment is appreciated.
5
817
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 flags (bits) in the stream, right? I solved it by declaring the stringstream inside the loop body even though I have need of another stringstream just after the loop. So for each iteration the constructor of the stringstream will be executed. Is...
8
1540
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 over to a string. This doesn't work. It misses data and teh result is not the same as when using char buffer, which gives correct download. Could anyone point out why the ostringstream + string way doesn't work, while the char buffer does? tia...
2
13991
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 the contents of the WebBrowser before I show the next page, they're appended one after the other. I've got some really simple code:
3
1403
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 still contains an empty string cout << strm.str(); // but the stream internally contains "25"
1
1163
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 tell me, what is the best way to clear the contents of a session object?
15
1955
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 " << 12.33 << " and a number";
13
2467
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
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10571
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.