【发布时间】:2015-08-07 05:26:01
【问题描述】:
我想测试 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如何保留换行符。