Solved

Clear stringStream before use

Posted on 2013-12-09
10
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 

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 34

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 34

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

Independent Software Vendors: 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 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.
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.

705 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