Solved

Clear stringStream before use

Posted on 2013-12-09
10
281 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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 32

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 32

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now