我放弃了,我只是想知道为什么push_back没有按预期工作。忘记我的代码有多糟糕或过于复杂。
我有一个python程序,它读取缓冲区,并从列表中逐行显示。如果最后一行未完成,请将其从列表中删除,并在下一次迭代时将其预先附加到缓冲区。
response += s.recv(1024).decode("utf-8")
if response[-1] != '\n':
lines = response.splitlines()
response = lines[-1]
lines = lines[:-1]
else:
lines = response.splitlines()
response = '';
for line in lines:
print("New Line: " + line)
time.sleep(1)
以" New Line"为前缀所以我可以看到我的"行"
中没有隐藏多行这是输出:
New Line: :tmi.twitch.tv 001 k20stitchbot :Welcome, GLHF!
New Line: :tmi.twitch.tv 002 k20stitchbot :Your host is tmi.twitch.tv
New Line: :tmi.twitch.tv 003 k20stitchbot :This server is rather new
New Line: :tmi.twitch.tv 004 k20stitchbot :-
New Line: :tmi.twitch.tv 375 k20stitchbot :-
New Line: :tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
New Line: :tmi.twitch.tv 376 k20stitchbot :>
New Line: :tmi.twitch.tv CAP * ACK :twitch.tv/membership
所以我用C ++编写了以下用C ++编写的以下内容:
found = start = end = 0;
iResult = recv(ConnectSocket, recvbuf, recvbuflen - 1, 0);
if (iResult > 0)
{
recvbuf[iResult] = '\0';
newString.append(recvbuf);
std::replace(newString.begin(), newString.end(), '\r', ' ');
end = newString.length();
while ((found = newString.find("\n", start)) != -1) {
myList.push_back(newString.substr(start, found));
start = found + 1;
if (start >= end) {
start = end;
}
Sleep(1000);
}
if (newString.back() != '\n')
{
std::string leftOver = newString.substr(start, end);
newString = leftOver;
}
else
{
newString = "";
}
for (myListIt = myList.begin(); myListIt != myList.end(); myListIt++)
{
cout << "New Line: " << *myListIt << "\n";
}
myList.clear();
}
我的输出不太理想,它几乎就好像不是从开始推到列表中,而是从头到尾推动。
New Line: :tmi.twitch.tv 001 k20stitchbot :Welcome, GLHF!
New Line: :tmi.twitch.tv 002 k20stitchbot :Your host is tmi.twitch.tv
:tmi.twitch.tv 003 k20stitchbot :This server is
New Line: :tmi.twitch.tv 003 k20stitchbot :This server is rather new
:tmi.twitch.tv 004 k20stitchbot :-
:tmi.twitch.tv 375 k20stitchbot :-
:tmi.twitch.tv 372 k20stitchbot :You
New Line: :tmi.twitch.tv 004 k20stitchbot :-
:tmi.twitch.tv 375 k20stitchbot :-
:tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 k20stitchbot :>
New Line: :tmi.twitch.tv 375 k20stitchbot :-
:tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 k20stitchbot :>
New Line: :tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 k20stitchbot :>
New Line: :tmi.twitch.tv 376 k20stitchbot :>
答案 0 :(得分:1)
子串参数是:begin-position,length
而不是:开始位置,结束位置