3

I have a string "1|4|1577|1|10.22.33|7001390280000019|||||172.20.5.20|1" and I want to split this string to get a result like this:

1
4
1577
10.22.33
7001390280000019
null
null
null
null
172.20.5.20
1

But when I use strtok in a while cycle, the pipes that doesn't have any content are not showing, so my result looks like this:

1
4
1577
1
10.22.33
7001390280000019
172.20.5.20
1

How can I get this result?

Here is my code:

int main(argc,argv)
int argc;
char *argv[];
{
    char *var1="1|4|1577|1|10.22.33|7001390280000019|||||172.20.5.20|1";
    char *var2=malloc(strlen(var1)+1);
    strcpy(var2,var1);
    while ((var2 = strtok(var2, "|")) != NULL){
        printf("<<%s>>\n", var2);
        var2= NULL;
    }
    return 0;
}

Thanks in advance

Alan Gaytan
  • 852
  • 4
  • 14
  • 33
  • 1
    use [strsep](http://manpages.ubuntu.com/manpages/vivid/en/man3/strsep.3.html) – BLUEPIXY Oct 26 '15 at 17:25
  • Thanks to all for answering my doubts, @BLUEPIXY Do you have an example using strsep?? – Alan Gaytan Oct 26 '15 at 17:37
  • Even if `strtok` was the right function (it isn't), the use of `var2` is quite wrong. First the `malloc` should be `char *var2=malloc(strlen(var1)+1);` but you then overwrite `var2=NULL` preventing it being `free`ed. Better would have been `var2=strdup(var1)` but you still would need `free(var2)`. – Weather Vane Oct 26 '15 at 17:38
  • @AlanGaytan the OS X man page (http://www.unix.com/man-page/osx/3/strsep/) for `strsep()` seems to have an example that addresses what you're doing :) – mah Oct 26 '15 at 17:39
  • Thank you @mah, let me check that example :) – Alan Gaytan Oct 26 '15 at 17:43
  • @WeatherVane how does strdup works??? :) – Alan Gaytan Oct 26 '15 at 17:43
  • @AlanGaytan you want a link to the `strdup` man page or you want me to explain it? – Weather Vane Oct 26 '15 at 17:45
  • @WeatherVane whichever is easier for you, and for the string terminator let me make the change :) – Alan Gaytan Oct 26 '15 at 17:50
  • You already made the change (as my deleted comment); and it's easier for me to let you research `strdup` yourself. – Weather Vane Oct 26 '15 at 17:51
  • [example](http://ideone.com/4t9JYp) – BLUEPIXY Oct 26 '15 at 18:04
  • thank you @BLUEPIXY but as the example provided below I have issue with the strsep, it show the error: undefined reference to `strsep' – Alan Gaytan Oct 26 '15 at 18:16
  • 2
    if your system does not have `strsep`, [sample implementation](http://stackoverflow.com/a/26399216/971127). or search code by google. – BLUEPIXY Oct 26 '15 at 18:21

2 Answers2

1

Here's an example on how this would work with strsep and strdup:

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

int main()
{
    char *var1="1|4|1577|1|10.22.33|7001390280000019|||||172.20.5.20|1";
    char *p, *var2, *var3;
    var2=strdup(var1);   // allocates enough space for var1 and copies the contents
    var3=var2;           // save off var2, since strsep changes it
    while ((p = strsep(&var2,"|")) != NULL) {   // p contains the token
        printf("<<%s>>\n", p);
    }
    free(var3);          // var2 is now NULL, so use var3 instead
    return 0;
}

Output:

<<1>>
<<4>>
<<1577>>
<<1>>
<<10.22.33>>
<<7001390280000019>>
<<>>
<<>>
<<>>
<<>>
<<172.20.5.20>>
<<1>>
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thans for the example but when I copy the code I get the next error: undefined reference to `strsep', Is something missing??? – Alan Gaytan Oct 26 '15 at 18:04
  • @AlanGaytan If that's the case then `strsep` is not available. What OS are you on, and what compiler are you using? – dbush Oct 26 '15 at 18:06
  • I'm using MinGW, I'm very new in C language so I'm not sure what is OS? – Alan Gaytan Oct 26 '15 at 18:14
  • 1
    @AlanGaytan If you're using MinGW, then you're on Windows, which doesn't support `strsep`. See the comment left by BLUEPIXY. – dbush Oct 26 '15 at 18:25
0

Thanks to @BLUEPIXY and @dbush the code finish like this:

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

char *strsep(char **sp, const char *sep);

int main()
{
    char *var1="1|4|1577|1|10.22.33|7001390280000019|||||172.20.5.20|1";
    char *p, *var2, *var3;
    var2=strdup(var1);   // allocates enough space for var1 and copies the contents
    var3=var2;           // save off var2, since strsep changes it
    while ((p = strsep(&var2,"|")) != NULL) {   // p contains the token
        printf("<<%s>>\n", p);
    }
    free(var3);          // var2 is now NULL, so use var3 instead
    return 0;
}

char *strsep(char **sp, const char *sep){
    char *p, *s;
    if (sp == NULL || *sp == NULL || **sp == '\0') return(NULL);
    s = *sp;
    p = s + strcspn(s, sep);
    if (*p != '\0') *p++ = '\0';
    *sp = p;
    return(s);
}

Result:

<<1>>
<<4>>
<<1577>>
<<1>>
<<10.22.33>>
<<7001390280000019>>
<<>>
<<>>
<<>>
<<>>
<<172.20.5.20>>
<<1>>
Alan Gaytan
  • 852
  • 4
  • 14
  • 33