Solved

Clear stringStream before use

Posted on 2013-12-09
10
291 Views
Last Modified: 2014-01-04
When I try to use hexify multiple time, it seems to hexify the input twice.  I believe I need to clear stringstream everytime when I use it. But how? Can anyone help me?

int main() {

	string s = "My error message\n My error message\n My error message\n My error message";
	stringstream ss;

	cout << for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;
	cout << for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;
	// Or, as in your case:
	//
	// log4cpp::Category::getRoot() <<  log4cpp::Priority::DEBUG << for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;

	return 0;
}

Open in new window

0
Comment
Question by:tommym121
  • 4
  • 4
  • 2
10 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 300 total points
ID: 39707729
You can just do that by using stringstream::str("") - e.g.

#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <iomanip>
using namespace std;


template<class T> struct hexify {

  hexify(stringstream& out) : ss(out), count(0) { ss.str(""); } // 'ss.str("");' clears the stream
  void operator() (T& x) { ss << setbase(16) << "0x" << (int) x << ' '; if(!((count +1 ) % 16)) ss << endl; ++count;}
  stringstream& ss;
  unsigned int count;
};

template<class T>
ostream& operator<<(ostream& os, const hexify<T>& h) { os << h.ss.str(); return os;}

int main() {

  string s = "My error message\n My error message\n My error message\n My error message";
  stringstream ss;

  cout <<  for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;

  // Or, as in your case:
  //
 // log4cpp::Category::getRoot() <<  log4cpp::Priority::DEBUG << for_each(s.begin(), s.end(), hexify<char>(ss)) << endl;

  return 0;
}
                                            

Open in new window

0
 

Author Comment

by:tommym121
ID: 39707739
JKR,

How can we initialize ss with out like hexify(stringstream& out) : ss(out), .....
And then clear the string. How can that work>
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 300 total points
ID: 39707762
You can always call 'stringstream::str("your content here")' whenever you need to adjust the contents of your stringstream and use all the manipulators hat have been a stringstream offers ;o)
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:tommym121
ID: 39707769
JKR.

Is it right ss(out) like calling fopen?  However stringstream::str() really adjust what in the pipeline, in this case, it is string stream.  Is this the right way to look at it. That is why you can initialize, before clean up the pipeline.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39707782
Sorry, but either me or you got a bit confused here.. how would 'fopen()' enter the stage?
0
 

Author Comment

by:tommym121
ID: 39708220
JKR,

I am sure I am the confused one and I got you confuse.  I mean ss(out) like simply attach 'out' to SS but does not copy 'out' content into ss:str().

Or maybe I should let you explain.
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 200 total points
ID: 39708824
you could set a stringstream ss to an initial state by

ss = std::stringstream();

Open in new window


note, for your purposes it is not necessary to provide a local stringstream object. the stringstream also could be provided by the functor struct as well (see struct hexedit in http:Q_28313598.html#a39708622).

Sara
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 300 total points
ID: 39708874
>>I mean ss(out) like simply attach 'out' to SS but does not copy 'out' content into ss:str().

Well, to 'attach' a stringstream to another stringstream without copying the contents is actually quite poinless. Another option would be to let 'hexify' handle it's own stringstream without passing one in - is that what you are thinking of?
0
 
LVL 33

Accepted Solution

by:
sarabande earned 200 total points
ID: 39709245
How can we initialize ss with out like hexify(stringstream& out) : ss(out), .....
And then clear the string. How can that work>
you could do it in the constructor of the hexify struct:

hexify(stringstream& out) : ss(out), count(0) { ss = std::stringstream(); }

Open in new window


the member ss is a reference to a stringstream not an own copy of the stringstream object. that means the ss actually "IS" the stringstream variable you defined in main function. the hexify object was created when you were calling the foreach function. then the constructor was called and the local stringstream passed was attached to the internal alias ss in the hexify object. that would/could be the right moment to initialize the temporary stringstream object (and not overtaking any old contents of the stringstream).

Sara
0
 

Author Closing Comment

by:tommym121
ID: 39755970
Thanks
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

778 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