2016-04-22 36 views
-3

当我测试C++ STL功能时,我有一个奇怪的问题。如果我取消注释该行(eee),则我的while循环不会退出。
我在64位Windows下使用vs2015。做这个stl操作符>>函数是否会发生魔法?

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; 
    } 
+0

@Christophe - 这怎么可能是完全相同的副本?这看起来很不同,因为使用了'偷看'和'失败'。此外,问题是关于与EOF无关的if语句。 – 4386427

+0

@ 4386427你是对的,它不是一个确切的副本。我重新打开了 – Christophe

+0

丹尼斯,你能解释一下它与标准模板库(STL)的关系吗? – Christophe

回答

1

我认为

int eee = myinput.peek(); 

在某个时刻返回零。

然后由于

if (eee) 

停止从流中读取,从不EOF到达。

尝试做

if (eee >= 0) 

代替

作为替代方案,你可以这样做:

if (eee < 0) 
    { 
     break; 
    } 

    // No need for further check of eee - just do the read 
    myinput >> myfunclist[i++]; 
+0

非常感谢。此外,它在istringstream中遇到\ r \ n时返回零,我使用这个if语句,因为xcode下的相同代码对myfunclist有额外的0输出,这很奇怪... xcode的stl版本具有不同的行为$美元的视觉工作室。当在vs下运行这段代码时,0不会输出到funclist,即使我不使用if语句。 (xcode版本是7.3) –