Solved

Clear stringStream before use

Posted on 2013-12-09
10
286 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

896 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

12 Experts available now in Live!

Get 1:1 Help Now