Search code examples
c++listpush-back

Funny behavior when pushing onto list in c++


I give up, I simply want to know why push_back is not working as intended. Forget how bad or over complicated my code is.

I have a python program which reads a buffer, and displays it line by line from a list. If the last line is not complete, remove it from the list and it will be pre-appended to the buffer on the next iteration.

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)

Prefixed with "New Line" so I can see that multiple lines are not hidden in my "line"

This is the output:

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    

So I've written the following in C++ with visual studios as my compiler:

    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();
    }

My Output here is less than desirable, it's almost as if instead of pushing from start to found onto the list, it's pushing from start to end.

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 :>

Solution

  • The substring arguments are: begin-position, length
    and not: begin-position, end-position