string splitting

X

xyz

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 this string such that i need to get all these
parameters....

thank you for any help
 
I

Ian Collins

xyz said:
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 this string such that i need to get all these
parameters....
So, what have you tried?

Whether or not the field widths are fixed will make a significant impact
on how you split the sting.
 
X

xyz

So, what have you tried?

Whether or not the field widths are fixed will make a significant impact
on how you split the sting.


the field widths are not fixed..i have some times four/three digits
srcport ..so i cant do it with substr function
i am not getting an idea how to split it..
thank you
 
X

xyz

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
 
F

fnegroni

There is a nasty function called strtok().
But you can most probably write your own implementation if you
understand what it is that you want to achieve:
1) skip all separators, no matter how many
2) null terminate all tokens (words, parameters)
3) remember the offset of each token.
 
X

xyz

There is a nasty function called strtok().
But you can most probably write your own implementation if you
understand what it is that you want to achieve:
1) skip all separators, no matter how many
2) null terminate all tokens (words, parameters)
3) remember the offset of each token.

I have a string
16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168
i have so many of such lines
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 this string such that i need to get all these
parameters....

thank you for any help
 
S

santosh

xyz said:
I have a string
16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168
i have so many of such lines
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 this string such that i need to get all these
parameters....

thank you for any help

Well, look closely at the string you have presented and you will see
that all unrelated elements are separated by a space. First you have
the time field with three components, each separated by the ':'
character. The total field length is 15 characters. Then comes the
combination of source address and port, this time the components are
separated by a '.' character and the last component specifies the
source port. The destination address and port field follows the same
organisation. The comes the protocol field which is usually TCP or
perhaps UDP. The remaining component specifies the payload size.

So to split this string to get the components in the manner you
indicate, splice the string at each whitespace character. Then for
second and third substrings that you so get split of all characters
that follow the fourth '.' character.

Note that the above method is totally hackish and fragile. If you want a
robust solution then you must study the protocol involved in detail and
either construct your own parser for it or use one of the many free
ones available on the Net.

Now try doing it and if the code is in C you can post here for further
help and comments. If you attempt it in C++ then post to comp.lang.c++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top