Solved

Clearing stringstream

Posted on 2001-08-06
11
887 Views
Last Modified: 2008-03-17
int main()
{
   string pnm;
   stringstream S;
   int x=123;
   int y=222;

   S << "xyz"  << x << "lpm" << y;
   
   pnm = S.str();
   cout << pnm << endl; // output should be xyz123lpm222

  // I would like to Clear 'S' - make it empty and reuse
  // to create another stream. (Modify its content)

   return 0;
}
0
Comment
Question by:prashant_n_mhatre
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6356883
I son't work a lot with stringstreams, but regarding the class hierarchy, calling 'S.flush()' should be enough...
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6356905
doesn't work...
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6356934
Thanks..I've figured out.

 S.seekp(0, ios::beg);

does that.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:Triskelion
ID: 6356964
Are you going to delete the question?
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6356973
Actually it will not work if I change the field length.
I need to add something like

freeze(false);

But that doesn't work with stringstream.
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 6356990
>> Thanks..I've figured out.
>> 
>> S.seekp(0, ios::beg);
>> 
>> does that.
Nope you didn't figure it out!  :-)

That makes a mess. (if you are not careful.

You need

stringstream S;
S.str(string());

or

S.str("");

continues
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6356992
Are you going to delete the question?
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6356999
That was an accident.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6357028
EE is not working well right now!   I was tryign to post fo the longest time. Anyways to contineu--hopefully.

As I see you now realize, the problem with

   S.seekp(0, ios::beg);

is that it just moves the write position back to the start of the stream.  But the data already written stays there, so if you do.

   S <<"1234567890";
   S.seekp(0);
   S << "ABC";
   string AString = S.str();

This leaves AString with "ABC4567890" instead of just "ABC".

Now there is an expert who will tell you that the correct solution is to that the fix for this is

   S <<"1234567890";
   S.seekp(0);
   S << "ABC" << char(0);
   string AString = S.str();

But do not be fooled!  This just sticks a NUL terminator in the middle of the string data so that it appears like "ABC" is all that is stored.  its not.  it still stores the "567890" after that.  In fact the string's length() function will report this.   Don't fall for this!

Instead make use of the fact that you can store a new string data sequence in the stream object with the overlaoded str() member function.  i.e. you can do

   S.str("ABC");

and now the stream stores "ABC".  I you read from the stream you will read "ABC"  If you write to the stream you will wrote after the "ABC"  (unless you seek around.)  

In your case you want to set the stream to store an empty string, like

   S.str(string());

New the stream is empty.  It stores no data to read from the stream.  It stores no data that will be combined with data written to the stream.

Note that I am not sure how this operation effects the stram position.  You might still need to seek to the start of the stream after clearing its contents.
0
 
LVL 22

Expert Comment

by:nietod
ID: 6357040
Okay, you shouldn't need to do a seekp(0) or seekg(0).  The str(string) member function will reset the read and write positions to 0.
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6357077
Thanks all!!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SetCurrentDirectory path limit 7 145
C++ Class Serialization, Encapsulation and Formatted Output 8 92
Path to  STL Map header file 1 82
C++ Code Issue 4 38
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question