Solved

Clear stringStream before use

Posted on 2013-12-09
10
296 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

821 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