0

I am having trouble getting istringstream to continue in while loop shown below. The data file is shown below also. I use getline from Input file to get the first line and put it in a istringstream lineStream. It passes through the while loop once, then it reads in the second line and goes back to the beginning of the loop and exits rather than continue through the loop. I have no clue why, if anyone could help I would thankful. EDIT: The reason I have this while loop condition is because the file may contain lines of erroneous data. Therefore, I want to make sure the line I am reading in has the proper form shown below in the data file.

while(lineStream >> id >> safety){//keeps scanning in xsections until there is no more xsection IDs

    while(lineStream >> concname){//scan in name of xsection
        xname = xname + " " +concname;
    }


    getline(InputFile, inputline);//go to next xsection line
    if(InputFile.good()){
        //make inputline into istringstream
        istringstream lineStream(inputline);
        if(lineStream.fail()){
            return false;
        }
    }
}

Data FILE

4   0.2  speedway and mountain
7   0.4 mountain and lee
6   0.5 mountain and santa
ddwong
  • 27
  • 1
  • 6
  • 1
    I think you have too many confusing file read operations that may not be coherent with each other. – Abhishek Bansal Dec 12 '13 at 07:27
  • lineStream needs to be declared at the top/global, not the bottom, or it will only be available in the block that is in – user2976089 Dec 12 '13 at 07:36
  • What's confusing about it? – ddwong Dec 12 '13 at 07:36
  • How is this supposed to work? Why did you make the istringstream at the end instead of at the beginning? Why would `fail()` return true when you didn't perform any operations except construction (which must succeed)? – Potatoswatter Dec 12 '13 at 07:46
  • It is at the end because I want to make sure I am reading in this certain type of data rather than erroneous data. I will edit original post. The 'fail()' does not return true. It skips over this and goes to the check of the while loop and then exits. – ddwong Dec 12 '13 at 07:49

1 Answers1

1

In the presented code, …

while(lineStream >> id >> safety){//keeps scanning in xsections until there is no more xsection IDs

    while(lineStream >> concname){//scan in name of xsection
        xname = xname + " " +concname;
    }

    getline(InputFile, inputline);//go to next xsection line
    if(InputFile.good()){
        //make inputline into istringstream
        istringstream lineStream(inputline);
        if(lineStream.fail()){
            return false;
        }
    }
}

… the inner declaration of lineStream declares a local object, which ceases to exist when the execution passes out of that block, and which doesn't affect the stream used in the outer loop.


One possible fix is to invert the code a little bit, like this:

while( getline(InputFile, inputline) )
{
    istringstream lineStream(inputline);

    if(lineStream >> id >> safety)
    {
        while(lineStream >> concname)
        {
            xname = xname + " " +concname;
        }
        // Do something with the collected info for this line
    }
}
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • No, I stated in my original comment: "I use getline from Input file to get the first line and put it in a istringstream lineStream. It passes through the while loop once..." Which means I declared lineStream outside of the whole loop, and then placed it in there. Unless you mean the inner declaration in the "if(InputFile.good()){} EDIT: I see what you mean. I'll try it out. – ddwong Dec 12 '13 at 07:35
  • I don't know how to explain it more clearly, sorry. The only additional suggestion I have then is to try to execute your current code in your head, just go through it mechanically step by step as the computer would have done, so that maybe you can convince yourself that the above explanation is meaningful and correct. – Cheers and hth. - Alf Dec 12 '13 at 07:40
  • Sorry, I edited my original comment after seeing your edit. I have already gone through it mechanically with a debugger and I am trying your approach. – ddwong Dec 12 '13 at 07:41
  • I edited a little more. The first changing an original `while` to an `if` doesn't affect execution, just clarity of the code. The second, moving the comment about doing things with the collected info to more appropriate place. :-) – Cheers and hth. - Alf Dec 12 '13 at 07:44
  • Thanks, it works now! I am just wondering though, why does the istringstream not keep its value at the end and through the next iteration? – ddwong Dec 12 '13 at 08:12
  • In the last code presented above, because the stream is a local variable in the loop body. It goes out of scope when the execution reaches the end of the loop body. Each time. – Cheers and hth. - Alf Dec 12 '13 at 09:42