string splitting plzzzzzz help me...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xyz

    string splitting plzzzzzz help me...

    I have a string
    16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 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
  • gNash

    #2
    Re: string splitting plzzzzzz help me...

    On Apr 29, 4:24 pm, xyz <lavanyaredd... @gmail.comwrote :
    I have a string
    16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 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...

    Comment

    • xyz

      #3
      Re: string splitting plzzzzzz help me...

      On Apr 29, 1:24 pm, xyz <lavanyaredd... @gmail.comwrote :
      I have a string
      16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 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 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

      Comment

      • vippstar@gmail.com

        #4
        Re: string splitting plzzzzzz help me...

        On Apr 29, 2:24 pm, xyz <lavanyaredd... @gmail.comwrote :
        I have a string
        16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 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++
        Then post in <news:comp.lang .c++>, as it is not topical in this group.
        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.

        Comment

        • Nick Keighley

          #5
          Re: string splitting plzzzzzz help me...

          On 29 Apr, 12:24, xyz <lavanyaredd... @gmail.comwrote :
          I have a string
          16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 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++


          --
          Nick Keighley

          Comment

          • Sumit

            #6
            Re: string splitting plzzzzzz help me...

            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("strPars e: %s\n", strParse);

            strParse = strtok(NULL, ".");

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

            strParse = strtok(NULL, ".");

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




            output :

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


            Comment

            • Barry Schwarz

              #7
              Re: string splitting plzzzzzz help me...

              On Tue, 29 Apr 2008 04:24:56 -0700 (PDT), xyz
              <lavanyareddy.p @gmail.comwrote :
              >I have a string
              >16:23:18.65934 3 131.188.37.230. 22 131.188.37.59.1 398 tcp 168
              >
              >for example lets say for the above string
              >16:23:18.65934 3 -- time
              >131.188.37.2 30 -- srcaddress
              >22 --srcport
              >131.188.37.5 9 --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

              Comment

              • Ricky

                #8
                Re: string splitting plzzzzzz help me...

                On Apr 29, 4:24 pm, xyz <lavanyaredd... @gmail.comwrote :
                I have a string
                16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 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

                Comment

                • John J. Smith

                  #9
                  Re: string splitting plzzzzzz help me...

                  xyz wrote:
                  I have a string
                  16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 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@d4 5g2000hsc.googl egroups.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.1 398 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.65934 3 131.188.37.230 22 131.188.37.59 1398 tcp 168";

                  void solve_problem(c har *, ...);

                  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[i] = ptr(w[i-1], ' ');
                  w[7] = ptr(w[6], '\0');

                  for(i = 0; i < 7; i++)
                  f[i] = wtof(w[i], w[i+1]-w[i]-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_s tring, 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(c har *problem, ...)
                  {
                  va_list ap;
                  va_start(ap, problem);
                  va_end(ap);
                  if(problem) {
                  fprintf(stderr, "dirty disk!\n");
                  abort();
                  }
                  }
                  /* end strspl.c */

                  --
                  John J. Smith
                  String Splitter

                  Comment

                  • Dieter =?ISO-8859-1?Q?B=FCrgi?=

                    #10
                    Re: string splitting plzzzzzz help me...

                    xyz wrote:
                    I have a string
                    16:23:18.659343 131.188.37.230. 22 131.188.37.59.1 398 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.65934 3 131.188.37.230. 22 131.188.37.59.1 398 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";
                    }


                    Comment

                    • Hans Schneider

                      #11
                      Re: string splitting plzzzzzz help me...

                      John J. Smith schrieb:
                      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@d4 5g2000hsc.googl egroups.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.1 398 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.65934 3 131.188.37.230 22 131.188.37.59 1398 tcp 168";
                      >
                      void solve_problem(c har *, ...);
                      >
                      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[i] = ptr(w[i-1], ' ');
                      w[7] = ptr(w[6], '\0');
                      >
                      for(i = 0; i < 7; i++)
                      f[i] = wtof(w[i], w[i+1]-w[i]-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_s tring, 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(c har *problem, ...)
                      {
                      va_list ap;
                      va_start(ap, problem);
                      va_end(ap);
                      if(problem) {
                      fprintf(stderr, "dirty disk!\n");
                      abort();
                      }
                      }
                      /* end strspl.c */
                      >

                      Comment

                      • Peter Nilsson

                        #12
                        Re: string splitting plzzzzzz help me...

                        Hans Schneider wrote:
                        John J. Smith schrieb:
                        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.
                        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.

                        --
                        Peter

                        Comment

                        • Keith Thompson

                          #13
                          Re: string splitting plzzzzzz help me...

                          Hans Schneider <hans@localhost .localdomainwri tes:
                          John J. Smith schrieb:
                          >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?
                          [...]

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

                          --
                          Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                          Nokia
                          "We must do something. This is something. Therefore, we must do this."
                          -- Antony Jay and Jonathan Lynn, "Yes Minister"

                          Comment

                          • CBFalconer

                            #14
                            Re: string splitting plzzzzzz help me...

                            Hans Schneider wrote:
                            John J. Smith schrieb:
                            >
                            >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?
                            Probably because LCC-WIN32 fails to observe the C standard. There
                            is no such function as "wtof" in the C library.

                            --
                            [mail]: Chuck F (cbfalconer at maineline dot net)
                            [page]: <http://cbfalconer.home .att.net>
                            Try the download section.


                            ** Posted from http://www.teranews.com **

                            Comment

                            • Eligiusz Narutowicz

                              #15
                              Re: string splitting plzzzzzz help me...

                              CBFalconer <cbfalconer@yah oo.comwrites:
                              Hans Schneider wrote:
                              >John J. Smith schrieb:
                              >>
                              >>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?
                              >
                              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.

                              Comment

                              Working...