Stringstream skipping first line

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrPickle
    New Member
    • Jul 2008
    • 100

    Stringstream skipping first line

    I have a tag like so:
    <foo>

    and I am trying to extract 'foo' from this.
    I'm using a stringstream in a while loop but it seems to fail with the first line, then it works with every other line.

    Code:
    std::ifstream is("xml.txt");
    	if(is.fail())
    	{
    		return 0;
    	}
    	std::string str, tag;
    	std::stringstream ss;
    
    	while(std::getline(is, str))
    	{
    		tag = "";
    		ss.seekg(std::stringstream::beg);
    		ss.str(str);
    		ss.ignore(256, '<');
    		ss >> tag;
    		std::cout << ss.str()<< " " << tag << "\n";
    	}
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    What happens when you swap lines #12 and #13?

    kind regards,

    Jos

    Comment

    • MrPickle
      New Member
      • Jul 2008
      • 100

      #3
      It works O_O

      Why does this make a difference?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by MrPickle
        It works O_O

        Why does this make a difference?

        The first time through the loop your stringstream has no string set to move to the begin of; it sets the stringstream to a fail status. After swapping those two lines it does have a string so it can move to the begin position thereof.

        I don't know by head if setting a new string makes the stream moves to its begin position; check it out; if it does you can remove that seekg alltogether.

        kind regards,

        Jos

        Comment

        Working...