Thread: stringstream with getline

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    29

    stringstream with getline

    Hey!

    I have been sitting with a problem for some time now, and cant figure out how it can be solved. I have some thoughts about it, and am gonna write that down after I posted the code.

    Code:
    string line;
    string word;
    std::stringstream strstream;
    while(!inout.eof())
    {
        getline(inout,line);
        strstream.clear();
        strstream.str(line);
        while(!strstream.eof())
       {
    	    strstream >> word;
                cout << "(" << word << ")";
       }
       cout << "_ENT_";
    }
    Lets say my file.txt is:
    ----
    one two three

    four five six


    seven
    ------
    it will write out:
    .......
    one two three
    three
    four five six
    six
    six
    seven
    ------------

    I made a Debug, step by step and went through it and saw where the problem is. It happens when it goes in this loop.
    Code:
    while(!strstream.eof())
    	{
                 strstream >> word;
                 cout << "(" << word << ")";
    	}
    when it reads the first line "one two three", it goes fine, but the problem appears when it reaches next line. Now when the next line is empty, the strstream dont see it as eof(), and therefor goes in the loop and print the last word "word" got.

    I know that the problem happens when it reaches a empty line, and that it doesnt happen if theres a string on that line.

    Now my problem is, can I get a tip on how to solve it?
    Last edited by lovelace; 03-31-2012 at 08:23 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should not use eof() as a termination condition for your loops. The eof() will not be set until after you try to read past the end of file. Use your read:
    Code:
    while(getline(inout,line))
    {
    Jim

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    29
    Quote Originally Posted by jimblumberg View Post
    You should not use eof() as a termination condition for your loops. The eof() will not be set until after you try to read past the end of file. Use your read:
    Code:
    while(getline(inout,line))
    {
    Jim
    Cheers!

    I solved it with this loop first.
    Code:
    while(getline(inout,line))
    {
    And inside the loop I used this!
    Code:
    while(strstream >> word)
    {
    So now its not putting more words then there is in words. Great.

    is this the way you ment? Or is there a easier way?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    is this the way you ment? Or is there a easier way?
    Possibly, but without more content I can't tell for certain.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-13-2011, 07:32 AM
  2. stringstream fun
    By Prediluted in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2011, 10:14 AM
  3. stringstream Help...
    By neogst in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2011, 10:23 AM
  4. Stringstream
    By lruc in forum C Programming
    Replies: 9
    Last Post: 03-23-2009, 02:27 PM
  5. std::stringstream
    By Magos in forum C++ Programming
    Replies: 2
    Last Post: 11-13-2004, 05:56 AM