--
Jim Langston
"xyz" <> wrote in message
news:8daf3ab2-37ef-47ac-b2b4-...
>I have a string
> 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168
>
> for example lets say for the above string
> 16:23:18.659343 -- time
> 131.188.37.230 -- srcaddress
> 22 --srcport
> 131.188.37.59 --destaddress
> 1398 --destport
> tcp --protocol
> 168 --size
> i need to split the string such that i need to get all these
> parameters....
> the field widths are not fixed..i have some times four/three digits
> srcport ..so i cant do it with substr function...i need this in c++
> i am not getting an idea how to split it..
> thank you for any help
Not complete but giving you all the pieces.
You should use your favorite method for converting from strings to ints,
I'im showing a manual stringstream way, but I use a template myself.
Output is:
16:23:18.659343 -- time
131.188.37.230.22 -- srcaddress/port
131.188.37.59.1398 -- destaddress/port
tcp -- protocol
168 -- size
131.188.37.230 : 22
#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::string Input( "16:23:18.659343 131.188.37.230.22 131.188.37.59.1398
tcp 168" );
std::stringstream Stream( Input );
std::string Time;
std::string SrcAddressPort;
std::string DestAddressPort;
std::string Protocol;
int Size;
if ( Stream >> Time >> SrcAddressPort >> DestAddressPort >> Protocol >>
Size )
{
std::cout << Time << " -- time\n" <<
SrcAddressPort << " -- srcaddress/port\n" <<
DestAddressPort << " -- destaddress/port\n" <<
Protocol << " -- protocol\n" <<
Size << " -- size\n\n";
}
else
std::cerr << "Parsing error\n";
std::string SrcAddress;
std::string PortString;
int SrcPort = 0;
SrcAddress = SrcAddressPort.substr( 0,
SrcAddressPort.find_last_of('.') );
PortString = SrcAddressPort.substr( SrcAddressPort.find_last_of('.') +
1, std::string::npos );
std::stringstream Convert;
Convert << PortString;
Convert >> SrcPort;
std::cout << SrcAddress << " : " << SrcPort << "\n";
}