String stream repeating first word?

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

    String stream repeating first word?

    I am tokenizing a line using a stringstream and a vector but the stringstream seems to be repeating the first word, here's my code:

    Code:
    std::stringstream ss;
    std::vector<std::string> LineVec;
    std::string Line, Word;
    while(std::getline(File, Line)) {
       if(Line.empty()) { continue; }
       ss.str(Line);
       ss.seekg(std::ios::beg);
       while(ss >> Word) { LineVec.push_back(Word); }
    
       //Un related stuff here
    
       LineVec.clear();
    }
    Here's an example input-output
    Input: Hello, My name's MrPickle
    Output: Hello, My name's MrPickle Hello,
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    This line
    while(ss >> Word) { LineVec.push_ba ck(Word); }
    reads words from ss until ss contains no more words- and then ss fails and won't read anything more. Try calling ss.clear() after you do this.
    Hope this helps.

    Comment

    • oler1s
      Recognized Expert Contributor
      • Aug 2007
      • 671

      #3
      Show us a complete, compileable, snippet of code that we can directly run (for example using std::cin and not a file) to see the problem for ourselves. Because I can construct based on what you did, and have it work just fine.

      Comment

      • MrPickle
        New Member
        • Jul 2008
        • 100

        #4
        Sorry, I had made it output the first word later on. I had forgotten about it and missed it when looking for the reason why the first word was being repeated.

        Sorry & thanks.

        Comment

        • boxfish
          Recognized Expert Contributor
          • Mar 2008
          • 469

          #5
          Originally posted by oler1s
          Because I can construct based on what you did, and have it work just fine.
          I did that, and it worked fine for the first line, but after that it stopped working because ss had failed.

          Comment

          Working...