Split Function für Packets?

10/06/2012 12:10 Mr.Zoey#1
Hallo ich brauch mal eure hilfe

Also ich müsste das:
Code:
4000 packet1 4001 packet2 4003 packet3
zu das haben:
Code:
4000 packet1
4001 packet2
4003 packet3
Ich schaff es aber einfach nicht ;(
10/06/2012 14:11 MoepMeep#2
regex.
10/06/2012 14:51 Nightblizard#3
Code:
#include <string>
#include <sstream>
#include <iostream>

struct PacketInfo
{
	unsigned long opcode;
	std::string name;
};


std::stringstream& operator>>(std::stringstream& ss, PacketInfo& packetinfo)
{
	ss >> packetinfo.opcode;
	ss >> packetinfo.name;

	return ss;
}



int main()
{
	std::string stuff = "123 packet1 234 packet2 345 packet3 456 packet4";
	std::stringstream ss;
	ss << stuff;

	PacketInfo packetinfo;
	while(ss >> packetinfo)
		std::cout << packetinfo.opcode << " " << packetinfo.name << std::endl;
}
Aber nur weil heute internationaler Copy/Paste Tag ist!
10/06/2012 23:32 daemon7777#4
Dass das mit StringStreams so einfach geht, wusst ich noch gar nicht. Bisher habe ich für so etwas eine kleine Funktion geschrieben, die jeweils das nächste Leerzeichen sucht. Das wäre eigentlich auch eine ganz gute Übung, für alle, die noch nicht so viel mit strings gearbeitet haben.
10/07/2012 15:49 ernilos#5
Quote:
Originally Posted by Nightblizard View Post
Code:
#include <string>
#include <sstream>
#include <iostream>

struct PacketInfo
{
	unsigned long opcode;
	std::string name;
};


std::stringstream& operator>>(std::stringstream& ss, PacketInfo& packetinfo)
{
	ss >> packetinfo.opcode;
	ss >> packetinfo.name;

	return ss;
}



int main()
{
	std::string stuff = "123 packet1 234 packet2 345 packet3 456 packet4";
	std::stringstream ss;
	ss << stuff;

	PacketInfo packetinfo;
	while(ss >> packetinfo)
		std::cout << packetinfo.opcode << " " << packetinfo.name << std::endl;
}
Aber nur weil heute internationaler Copy/Paste Tag ist!
I tryed it on my server and don't work..
[Only registered and activated users can see links. Click Here To Register...]
><
10/07/2012 19:04 Nightblizard#6
Well, then you're doing it wrong. The incoming string has to be in this format:
Code:
number<whitespace>name<whitespace>number<whitespace>string ...
Your code is needed in order to give further details.
10/08/2012 18:34 ernilos#7
I tryed to write my own function
Code:
std::vector<std::string> splitPacket(string str)
{
	int temp;
	int cont;
	int stemp;
	std::vector<std::string> Packet = split(str,' ');
	std::vector<std::string> rPacket;
	temp = atoi(Packet[0].c_str());
	for(int i = 1; i < rPacket.size(); i++)
	{
		stemp = atoi(Packet[i].c_str());
		if(temp == stemp +1)
		{
			cont++;
			temp++;
		}
		else
		{
			cout << Packet[i].c_str() << endl;
			rPacket[cont] += Packet[i];
		}
	}
	return rPacket;
}
but don't works.. ><
10/08/2012 20:41 Nightblizard#8
This doesn't even look close to the code I've posted...
Why are you doing it this way? It looks ugly as hell, is bug prone and not easy maintainable.

Furthermore, atoi is old C rubbish and not safe at all. You sould use stringstreams for that aswell!
Doing so will allow you stuff like this:
[Only registered and activated users can see links. Click Here To Register...]

This is by far the better solution.