reset a stringstream

This is a discussion on reset a stringstream within the C++ Programming forums, part of the General Programming Boards category; I googled and googled, but i can't get any of the recommedations to work: i'm trying to cout a graph ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    43

    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???
    Code:
    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;
    			flag=true;
    		}
    		else if (sstream >> str)
    			
    			out << ',' << *current;
    
    		current=current->getNext();
    
                    /* the following are some the ways I tried to reset the stream!!
    		sstream.clear();
    		sstream.seekp(0);
    		sstream << '\0';
    	}
    	out << '|';
    	return out;

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

    --
    Mats
    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
    Posts
    43
    good idea!!!
    thanks a million

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


    Code:
    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, 12: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21