-3

I have a weird problem when I test the c++ STL features. If I uncomment the line if(eee), my while loop never exits.
I'm using vs2015 under 64-bit Windows.

    int i = 0;
    istream& mystream = data.getline(mycharstr,128);
    size_t mycount = data.gcount();
    string str(mycharstr,mycharstr+mycount);
    istringstream myinput(str);
    WORD myfunclist[9] = {0};
    for_each(myfunclist,myfunclist+9, [](WORD& i){ i = UINT_MAX;});
    CALLEESET callee_set;
    callee_set.clear();
    bool failbit = myinput.fail();
    bool eof = myinput.eof();
    while (!failbit && !eof)
    {
        int eee = myinput.peek();
        if (EOF == eee) break;
        //if (eee) // if i uncomment this line ,the failbit and eof will always be false,so the loop will never exit.
        {
            myinput >> myfunclist[i++];
        }
        //else break;
        failbit = myinput.fail();
        eof = myinput.eof();
        cout << myinput.rdstate() << endl;
    }
Prune
  • 72,213
  • 14
  • 48
  • 72
  • @Christophe - How can this be an exact duplicate? It seems very different as both `peek` and `fail` are used. Further, the question is about an if-statement not related to EOF. – 4386427 Apr 22 '16 at 17:00
  • @4386427 You're right it's not an exact duplicate. I reopened – Christophe Apr 22 '16 at 17:04
  • Dennis, can you please explain how this relates to the Standard Template Library (STL) ? – Christophe Apr 22 '16 at 17:05

1 Answers1

1

I think that

int eee = myinput.peek();

at some point returns zero.

Then due to

if (eee)

you stop reading from the stream and never reach EOF.

Try to do

if (eee >= 0)

instead

As an alternative you could do:

    if (eee < 0)
    {
        break;
    }

    // No need for further check of eee - just do the read
    myinput >> myfunclist[i++];
4386427
  • 33,845
  • 4
  • 32
  • 53
  • thanks so much. furthermore, it does return a zero when meet the \r\n in the istringstream, i using this if statement because the same code under xcode have a extra 0 output to myfunclist, it's weird ... the xcode's stl version have different behavior with $ms visual studio. when running this code under vs , the 0 never output to funclist even i do not use this if statement. (xcode version is 7.3) – Bing Dennis Apr 23 '16 at 00:05