string splitting plzzzzzz help me...

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

gNash

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

the clue is '.' so read function as many you want according to each
field...
 
V

vippstar

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..
You could start by splitting the spaces. Then determine which from the
splitted tokens are ipv4 addresses (do you care about ipv6?) and
strrchr(str, '.')
The solution seems very easy. I suggest you go back to learning the
basics before you start writing networking code.
 
N

Nick Keighley

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

strtok(), sscanf()?
operator>>() if you insist on C++
 
S

Sumit

On Tue, 29 Apr 2008 06:42:59 -0700, Nick Keighley wrote:

This group is for C only:
take help from this example and convert this to C++

parsing of : comp.lang.c

#include<stdio.h>
#include<string.h>

int main()
{
char str[] = "comp.lang.c";
char *strParse;

printf("\n %s \n", str);

strParse = strtok(str, ".");

printf("strParse: %s\n", strParse);

strParse = strtok(NULL, ".");

printf("strParse: %s\n", strParse);

strParse = strtok(NULL, ".");

printf("strParse: %s\n", strParse);
return 0;
}




output :

comp.lang.c
strParse: comp
strParse: lang
strParse: c
 
B

Barry Schwarz

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

You have posted the same post four times in an hour. You are not
going to increase the level of response by being obnoxious. Show what
code you have developed so far and many will provide additional help.
Ask us to do the work for you and suffer in silence.


Remove del for email
 
R

Ricky

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

hi,
You can also use space (' ') to break the original strings and then
parse on the individual strings (after separating it on space taken)
to get your results.
- Ricky
 
N

Nick Keighley

This group is for C only:

but the poster asked for a c++ answer. I don't
consider "use operator>>()" to be massive abuse
of topicality.

Why did you leave in "Nick Keighleywrote" and then
quote nothing I wrote?
take help from this example and convert this to C++

<snip smart ass stuff>
 
J

John J. Smith

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

Well, I think plzzzzzz retired a few years ago, but maybe I can help.

Find below the portable C code of a program that splits a string very
similar to the one above into its components.

Enjoy.

/*
* strspl.c
*
* Copyright (c) 2008, John J. Smith
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose except in homework assignments
* is hereby granted without fee, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING AND NOT LIMITED TO ANY IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/

/*
* PROBLEM:
*
* Message-ID: \
* <0daad6a6-8f9d-453f-849b-511e88f094a6@d45g2000hsc.googlegroups.com>
* From: xyz <lavanyareddy[dot]p[at]gmail.com>
*
* 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
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

static const char *example_string =
"16:23:18.659343 131.188.37.230 22 131.188.37.59 1398 tcp 168";

void solve_problem(char *, ...);

static const char *ptr(const char *p, char c)
{
if(!c) return p + strlen(p) + 1;
while(*p && *p != c) p++;
while(*p == c) p++;
if(!*p)
solve_problem("character %c not found in string %s!", c, p);
return p;
}

static void *xmalloc(size_t sz)
{
char *new = malloc(sz);
if(!new)
solve_problem("unable to allocate %lu bytes of memory", (unsigned long)sz);
*(sz - 1 + new) = '\0';
return new;
}

static char *wtof(const char *w, size_t size)
{
char *f;
while(w[size-1] == ' ') size--;
f = xmalloc(size+1);
memcpy(f, w, size);
return f;
}

void split(
const char *string,
char **blue_goo,
char **green_goo,
char **purple_goo,
char **red_goo,
char **glowing_goo,
char **foo_goo,
char **goo_goo )
{
const char *w[8]; char *f[7]; int i;

w[0] = string;
for(i = 1; i < 7; i++)
w = ptr(w[i-1], ' ');
w[7] = ptr(w[6], '\0');

for(i = 0; i < 7; i++)
f = wtof(w, w[i+1]-w-1);

*blue_goo = 0[f]; *green_goo = f[1]; *purple_goo = 2[f];
*red_goo = f[3]; *glowing_goo = 4[f]; *foo_goo = f[5];
*goo_goo = 6[f];
}

int main(void)
{
char *p[7]; int i;

fprintf(stdout,
"splitting \"%s\",\n"
" please wait...\n",
example_string);
split(example_string, p+0, 1+p, p+2, 3+p, p+4, 5+p, p+6);

fprintf(stdout,
"result is\n"
" time: \"%s\"\n"
" src addr: \"%s\"\n"
" src port: \"%s\"\n"
" dst addr: \"%s\"\n"
" dst port: \"%s\"\n"
" protocol: \"%s\"\n"
" size: \"%s\"\n",
0[p], p[1], 2[p], p[3], 4[p], p[5], 6[p]);

fprintf(stdout, "freeing memory...\n");
for(i = 0; i < 7; i++)
free(i[p]);

fprintf(stdout, "all done.\n");
return 0;
}

void solve_problem(char *problem, ...)
{
va_list ap;
va_start(ap, problem);
va_end(ap);
if(problem) {
fprintf(stderr, "dirty disk!\n");
abort();
}
}
/* end strspl.c */
 
D

Dieter Bürgi

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

Easy!

#! /usr/bin/perl -W

$string = '16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168';

sub _split($) {
my ( $s ) = @_;
if( $s =~ / ^(\d+:\d+:\d+\.\d+)\ #
(\d+\.\d+\.\d+\.\d+)\.(\d+)\ #
(\d+\.\d+\.\d+\.\d+)\.(\d+)\ #
(\w+)\ #
(\d+)$
/x ) {
return ( $1, $2, $3, $4, $5, $6, $7 );
} else {
return ();
}
}

my @res = _split $string ;
foreach( @res ) {
print "$_\n";
}
 
H

Hans Schneider

John said:
Find below the portable C code of a program that splits a string very
similar to the one above into its components.

This program has 2 errors! It compiles not with LCC-WIN32.

lc -ansic -A -shadows -unused -O -c strspl.c -o strspl.obj
Error strspl.c: 73 redefinition of 'wtof'
Error c:\lcc\include\stdlib.h: 55 Previous definition of 'wtof' here
Warning strspl.c: 73 inconsistent linkage for 'wtof' previously declared at c:\lcc\include\stdlib.h 55
2 errors, 1 warning
1 error

You are redefining a function in stdlib.h

Why are your programs always not working with LCC-WIN32?

Enjoy.

/*
* strspl.c
*
* Copyright (c) 2008, John J. Smith
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose except in homework assignments
* is hereby granted without fee, provided that the above copyright
* notice appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING AND NOT LIMITED TO ANY IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/

/*
* PROBLEM:
*
* Message-ID: \
* <0daad6a6-8f9d-453f-849b-511e88f094a6@d45g2000hsc.googlegroups.com>
* From: xyz <lavanyareddy[dot]p[at]gmail.com>
*
* 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
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>

static const char *example_string =
"16:23:18.659343 131.188.37.230 22 131.188.37.59 1398 tcp 168";

void solve_problem(char *, ...);

static const char *ptr(const char *p, char c)
{
if(!c) return p + strlen(p) + 1;
while(*p && *p != c) p++;
while(*p == c) p++;
if(!*p)
solve_problem("character %c not found in string %s!", c, p);
return p;
}

static void *xmalloc(size_t sz)
{
char *new = malloc(sz);
if(!new)
solve_problem("unable to allocate %lu bytes of memory", (unsigned
long)sz);
*(sz - 1 + new) = '\0';
return new;
}

static char *wtof(const char *w, size_t size)
{
char *f;
while(w[size-1] == ' ') size--;
f = xmalloc(size+1);
memcpy(f, w, size);
return f;
}

void split(
const char *string,
char **blue_goo,
char **green_goo,
char **purple_goo,
char **red_goo,
char **glowing_goo,
char **foo_goo,
char **goo_goo )
{
const char *w[8]; char *f[7]; int i;

w[0] = string;
for(i = 1; i < 7; i++)
w = ptr(w[i-1], ' ');
w[7] = ptr(w[6], '\0');

for(i = 0; i < 7; i++)
f = wtof(w, w[i+1]-w-1);

*blue_goo = 0[f]; *green_goo = f[1]; *purple_goo = 2[f];
*red_goo = f[3]; *glowing_goo = 4[f]; *foo_goo = f[5];
*goo_goo = 6[f];
}

int main(void)
{
char *p[7]; int i;

fprintf(stdout,
"splitting \"%s\",\n"
" please wait...\n",
example_string);
split(example_string, p+0, 1+p, p+2, 3+p, p+4, 5+p, p+6);

fprintf(stdout,
"result is\n"
" time: \"%s\"\n"
" src addr: \"%s\"\n"
" src port: \"%s\"\n"
" dst addr: \"%s\"\n"
" dst port: \"%s\"\n"
" protocol: \"%s\"\n"
" size: \"%s\"\n",
0[p], p[1], 2[p], p[3], 4[p], p[5], 6[p]);

fprintf(stdout, "freeing memory...\n");
for(i = 0; i < 7; i++)
free(i[p]);

fprintf(stdout, "all done.\n");
return 0;
}

void solve_problem(char *problem, ...)
{
va_list ap;
va_start(ap, problem);
va_end(ap);
if(problem) {
fprintf(stderr, "dirty disk!\n");
abort();
}
}
/* end strspl.c */
 
P

Peter Nilsson

Hans said:
This program has 2 errors! It compiles not with LCC-WIN32.

Lcc-win32, or at least your copy of it, has errors.
lc -ansic -A -shadows -unused -O -c strspl.c -o strspl.obj
Error strspl.c: 73 redefinition of 'wtof'
Error c:\lcc\include\stdlib.h: 55 Previous definition of 'wtof' here
Warning strspl.c: 73 inconsistent linkage for 'wtof' previously
declared at c:\lcc\include\stdlib.h 55
2 errors, 1 warning
1 error

You are redefining a function in stdlib.h

No, the lcc standard library is breaking conformance by declaring
and defining a function outside of the implementation namespace.
That is not John's fault.
 
K

Keith Thompson

Hans Schneider said:
This program has 2 errors! It compiles not with LCC-WIN32.

lc -ansic -A -shadows -unused -O -c strspl.c -o strspl.obj
Error strspl.c: 73 redefinition of 'wtof'
Error c:\lcc\include\stdlib.h: 55 Previous definition of 'wtof' here
Warning strspl.c: 73 inconsistent linkage for 'wtof' previously declared at c:\lcc\include\stdlib.h 55
2 errors, 1 warning
1 error

You are redefining a function in stdlib.h

Why are your programs always not working with LCC-WIN32?
[...]

"wtof" is not defined in the C standard, and a conforming
implementation is not allowed to declare it in <stdlib.h>.

Perhaps this is corrected in a newer version of lcc-win32? As a
workaround, you can change the name "wtof" to something else.
 
E

Eligiusz Narutowicz

CBFalconer said:
Probably because LCC-WIN32 fails to observe the C standard. There
is no such function as "wtof" in the C library.

But it is still valid C I think. This is a C news group.
 
J

jacob navia

Richard said:
Eligiusz Narutowicz said:


Um, yes - that's the problem. The program had a wtof function. The name
'wtof' is in user namespace, and no C implementation is permitted to
invade that space. The lcc-win32 implementation, by doing so, fails
(again!) to conform to the definition of the C language. This is one of
many lcc-win32 conformance issues to be posted here in a comparatively
short time, and the maintainer's attitude seems to be that he only fixes
bugs reported by paying customers.

You are lying, as always.
 
J

John J. Smith

Hans said:
This program has 2 errors! It compiles not with LCC-WIN32.

lc -ansic -A -shadows -unused -O -c strspl.c -o strspl.obj
Error strspl.c: 73 redefinition of 'wtof'
Error c:\lcc\include\stdlib.h: 55 Previous definition of 'wtof' here

(?)
Aren't these two diagnostics referring to the same problem?

Why is the implementation in question *defining* (rather than declaring)
things inside a standard header?

On second thought, the second diagnostic appears to be reporting a
problem in "c:\lcc\include\stdlib.h" at line 55.

Warning strspl.c: 73 inconsistent linkage for 'wtof' previously declared\
at c:\lcc\include\stdlib.h 55
2 errors, 1 warning
1 error
^^^^^^^
What is this third error?

You are redefining a function in stdlib.h

Seems like I do, but I don't think <stdlib.h> should be
declaring/defining a symbol of that name; at least not if
the compiler is invoked in a standards-conforming mode.
(Out of curiosity, what is wtof() documented to be doing?)

Why are your programs always not working with LCC-WIN32?

To be honest, that's a good question. I have no idea.

Since my main development machine is a Sun4 (plus a Sun3 and
SGI Indy and a couple of historic unices), I'm afraid I can't
easily test my code on lcc-win without jumping through lots of
hoops (As it seems the free download is a Win32 executable, I
can't even unpack it and have a look at the included <stdlib.h>).

Perhaps I should add another disclaimer to the effect that
although the code is standards conform, it may or may not be
compatible with every C implementation out there.

[snip code you quoted but didn't comment on]
 
L

lawrence.jones

John J. Smith said:
Why is the implementation in question *defining* (rather than declaring)
things inside a standard header?

It's not -- the compiler's error messages are misleading.
(Out of curiosity, what is wtof() documented to be doing?)

Presumably, it's the wide character version of atof() -- it converts a
wide string to a double. It seems to be a Microsoftism, but recent
versions of MSC at least seem to spell it _wtof(), which avoids
polluting the user's namespace.

Because LCC-WIN32 doesn't conform to the C Standard in lots of little
ways. Whether that's due to ignorance, carelessness, or just
prioritization relative to other things, I'll leave to you to judge.

-- Larry Jones

Please tell me I'm adopted. -- Calvin
 
T

teapot

Keith said:
Perhaps this is corrected in a newer version of lcc-win32?

I keep reading that some problems may be corrected in newer versions,
but for some time now (at least since early 2008) I am unable to
download anything lcc-win32 related.

The link in J.N.'s signature leads to

[1] <http://www.cs.virginia.edu/~lcc-win32/> .


If I click on "lcc-win32" further down on that page, I'm taken to

[2] <http://www.q-software-solutions.de/downloaders/get_name> ,

which has a button at the bottom labeled "Get me to the Downloads".


Clicking on that opens up

[3] <http://www.q-software-solutions.de/downloaders/show_download_locations> ,

but choosing any download but "Changes" takes me back to [2], where
clicking on "Get me to the Downloads" opens up [3] again, etc, etc, etc...
At least from where I am there is no way to get a newer version.

Where did lcc-win32 users in c.l.c download the current free version?
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top