我想测试var字符串是否以'\ n'结尾,以检测并从流异步套接字中删除一些不完整的传入变量。似乎以下代码无法正常工作。为什么呢

string var;
char *rest = nullptr;
char *pVar = nullptr;
istringstream iss(sReadBuffer); // Put into a stream
while (getline(iss, var)) // Default delimiter '\n' or EOF
{
    int size = var.size();
    pVar = _strdup(var.c_str()); // Cast string to char * for later use (strstr,...)

    if(var[size] != '\n') // If incomplete variable found (not newline ended)...
    {
        debug("Incomplete variable found : ", pVar, "\n");
        rest = pVar;
        break;
    }

//... proceed with variable normally if they are complete

最佳答案

getline()放弃换行符。如果要测试EOF,请测试stringstream对象本身。

10-07 12:16