Thread: reset a stringstream

  1. #1
    Registered User
    Join Date
    Jan 2009

    reset a stringstream

    I googled and googled, but i can't get any of the recommedations to work:
    i'm trying to cout a graph in a formated way,
    it work fine for the first vertex, (prints all the outgoing edges, but when i get to the second one, the stringstream doesnt accecpt more data) how do I reset it???
    std::ostream& operator << (std::ostream & out,const Graph & g2)
    	std::string str="anything_quite_long";
    	stringstream sstream;
    	Node* current=g2.m_list->getHead();
    	bool flag=false;
    	//print list of vertices
    	out << '|' << *(g2.m_list)  << '|';
    	//go through list of edges
    	while (current!=NULL)
    		////prints all edges for current vertex
    		sstream << *current;
    		if (sstream >> str && flag==false)
    			out << *current;
    		else if (sstream >> str)
    			out << ',' << *current;
                    /* the following are some the ways I tried to reset the stream!!
    		sstream << '\0';
    	out << '|';
    	return out;

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Move the sstream variable declaration inside the while-loop, that way you get a "new" stringstream each time.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    good idea!!!
    thanks a million

  4. #4
    Registered User
    Join Date
    Oct 2006
    another option, which I use all the time is to use the str() member function of stringstream.

    std::stringstream ss;
    ss << "Hello, World";
    std::cout << ss.str() << std::endl; // with no arguments stringstream::str() returns the string as an std::string
    ss.str("foo"); // with a const char* argument, it sets the stringstream to the indicated value, which could be an empty string ("")
    std::cout << ss.str() << std::endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is there a way to reset the bash or zsh idle time?
    By Overworked_PhD in forum Linux Programming
    Replies: 3
    Last Post: 04-06-2009, 09:02 AM
  2. stringstream clear
    By misterowakka in forum C++ Programming
    Replies: 3
    Last Post: 01-01-2008, 01:03 PM
  3. strtok reset
    By lavinpj1 in forum C Programming
    Replies: 3
    Last Post: 04-25-2006, 06:00 PM
  4. Reloading Meshes on Device Reset
    By Epo in forum Game Programming
    Replies: 6
    Last Post: 05-30-2005, 04:21 PM
  5. Direct3D won't reset
    By Magos in forum Game Programming
    Replies: 1
    Last Post: 05-10-2004, 01:23 PM