我想从连续的字符串数据流中提取线。但数据格式不正确,我想放弃提取的行,以便内存不会超出界限。从连续的字符串流中提取线
例如,我有串行端口输出,诸如这样的:
批次1
POS 30 10 20\n
POS 30 10 21\n
POS 30 1
批次2
0 22\n
POS 30 10 23\n
PO
有一种方法,以有效地保存数据和提取线从中?
这是我的代码:
stringstream ss;
while (SP->IsConnected())
{
string result = SP->ReadData();
ss << result;
string oneline;
while(getline(ss, oneline))
{
cout << oneline << endl;
// or do something more interesting...
}
}
但自从我的函数getline代码不起作用()修改字符串流,使没有更多的数据可以被插入到它,我想保持stringstream的内存小了。所以我不认为我可以使用getline()并使用类似'popline()'的东西(如果存在)。
不确定,但尝试在内部'while()'循环后调用'ss.clear()'? –
为什么不使用'std :: deque'来保存行(每个都是'std :: string')?你想要的是一个“循环缓冲区”,并且流类没有被设计为表现这种方式。 (在标准库中也没有其他的东西。) – davidbak