<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: strtok_r return value.............. in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858516#M98406</link>
    <description>&lt;!--!*#--&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;dealing with trailing empty fields where left as an execise ...; (leading (multiple) empty fields can be done in a similar manner).&lt;BR /&gt;My solution stores the value of the initial string length and loops over the part skipped by strtok after its loop:&lt;BR /&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STRING.H&gt;&lt;BR /&gt;&lt;BR /&gt;int main (int argc, char** argv)&lt;BR /&gt;{&lt;BR /&gt;char*str = argv[1];&lt;BR /&gt;&lt;BR /&gt;char *pch, *last;&lt;BR /&gt;int i=0,j,d,l,ll;&lt;BR /&gt;&lt;BR /&gt;if (argc &amp;lt;2) return(1);&lt;BR /&gt;printf ("Splitting string \"%s\" in tokens:\n",str);&lt;BR /&gt;/* to get traling empty fields: */&lt;BR /&gt;ll=strlen(str);&lt;BR /&gt;&lt;BR /&gt;pch = strtok (str,":");&lt;BR /&gt;/* so you get 0 if no string is found */&lt;BR /&gt;if (pch) i++;&lt;BR /&gt;printf("%d %s\n",i,str);&lt;BR /&gt;&lt;BR /&gt;if(!pch) return(0);&lt;BR /&gt;&lt;BR /&gt;l = strlen(pch);&lt;BR /&gt;last = pch;&lt;BR /&gt;&lt;BR /&gt;while (pch = strtok(NULL,":"))&lt;BR /&gt;{&lt;BR /&gt;   d = pch - last;&lt;BR /&gt;   i++;&lt;BR /&gt;&lt;BR /&gt;/*&lt;BR /&gt;   if((d-l)&amp;gt;1) printf("%d empty fields\n",d-l-1);&lt;BR /&gt;*/&lt;BR /&gt;   for(j=d-l;j&amp;gt;1;j--) printf("%d\n",i++);&lt;BR /&gt;&lt;BR /&gt;   l=strlen(pch);&lt;BR /&gt;   if(l) printf("%d %s\n",i,pch);&lt;BR /&gt;   last=pch;&lt;BR /&gt;}&lt;BR /&gt;/*&lt;BR /&gt;printf("trailing empty fields: %d\n", str+ll-last-l);&lt;BR /&gt;*/&lt;BR /&gt;for(j=0;j&lt;STR&gt;&lt;/STR&gt;return 0;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;mfG Peter&lt;/STRING.H&gt;&lt;/STDIO.H&gt;</description>
    <pubDate>Wed, 13 Sep 2006 09:30:18 GMT</pubDate>
    <dc:creator>Peter Nikitka</dc:creator>
    <dc:date>2006-09-13T09:30:18Z</dc:date>
    <item>
      <title>strtok_r return value..............</title>
      <link>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858508#M98398</link>
      <description>Hello&lt;BR /&gt;  I have string abc:def::::lkm. I am using strtok_r to find the substring and storing the value returned by it.&lt;BR /&gt;&lt;BR /&gt;but strtok_r is not returning any value if their is nothing between delimiter.&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STRING.H&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;int main()&lt;BR /&gt;{&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;    char buffer[80], temp[80];&lt;BR /&gt;    char *sep = ":";&lt;BR /&gt;    char *brkt;&lt;BR /&gt;&lt;BR /&gt;    char *brkb;&lt;BR /&gt;&lt;BR /&gt;    char *word;&lt;BR /&gt;&lt;BR /&gt;    char *phrase;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;    strcpy(buffer, "This::abc:");&lt;BR /&gt;&lt;BR /&gt;    word = strtok_r(buffer, sep, &amp;amp;brkt);&lt;BR /&gt;&lt;BR /&gt;    printf("word : %s\n",word);&lt;BR /&gt;    printf("brkt : %s\n",brkt);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;    word = strtok_r(NULL, sep, &amp;amp;brkt);&lt;BR /&gt;    &lt;BR /&gt;    printf("word : %s\n",word);&lt;BR /&gt;    printf("brkt : %s\n",brkt);&lt;BR /&gt;&lt;BR /&gt;    return 0;&lt;BR /&gt;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Output is &lt;BR /&gt;$ ./abc&lt;BR /&gt;word : This&lt;BR /&gt;brkt : :abc:&lt;BR /&gt;word : abc&lt;BR /&gt;brkt :&lt;BR /&gt;&lt;BR /&gt;Well I need is that it should return me Null if their is no string between two delimiter.Is their is any better way to handle it. &lt;BR /&gt;&lt;BR /&gt;Hope i am clear.&lt;BR /&gt;&lt;BR /&gt;&lt;/STRING.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Thu, 07 Sep 2006 09:04:07 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858508#M98398</guid>
      <dc:creator>Tnameh</dc:creator>
      <dc:date>2006-09-07T09:04:07Z</dc:date>
    </item>
    <item>
      <title>Re: strtok_r return value..............</title>
      <link>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858509#M98399</link>
      <description>What you need and what strtok() or _r() do are two different things --- and the functions are working as designed. If either worked as you say you wanted then you would not be able to tell if you were at the end of the source string or simply between two adjacent tokens. What you should be testing for is that the ptr returned != NULL and, if so, that strlen(ptr) &amp;gt; 0. &lt;BR /&gt;&lt;BR /&gt;You are really confusing a NULL pointer and a null string -- and they aren't the same thing at all.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 07 Sep 2006 09:43:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858509#M98399</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-09-07T09:43:25Z</dc:date>
    </item>
    <item>
      <title>Re: strtok_r return value..............</title>
      <link>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858510#M98400</link>
      <description>&lt;BR /&gt;Thanks for the reply.&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STRING.H&gt;&lt;BR /&gt;&lt;BR /&gt;int main ()&lt;BR /&gt;{&lt;BR /&gt;  char str[] ="This:is::::::abc";&lt;BR /&gt;  char * pch;&lt;BR /&gt;  int i=0;&lt;BR /&gt;  printf ("Splitting string \"%s\" in tokens:\n",str);&lt;BR /&gt;  pch = strtok (str,":");&lt;BR /&gt;  while (pch != NULL)&lt;BR /&gt;  {&lt;BR /&gt;    pch = strtok(NULL,":");&lt;BR /&gt;    if( pch !=NULL) {&lt;BR /&gt;      if(strlen(pch) &amp;gt; 0)&lt;BR /&gt;        printf(" %d  %s\n",i++,pch);&lt;BR /&gt;     }&lt;BR /&gt;   else&lt;BR /&gt;    printf(" %d \n",i++);&lt;BR /&gt;  }&lt;BR /&gt;  return 0;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;What i need is when i parse string, i need  sequential data,even strlen(pch) is zero.&lt;BR /&gt;something like&lt;BR /&gt;1 This&lt;BR /&gt;2 is&lt;BR /&gt;3 &lt;BR /&gt;4 &lt;BR /&gt;5 &lt;BR /&gt;6 &lt;BR /&gt;7 &lt;BR /&gt;8 abc&lt;BR /&gt;&lt;BR /&gt;Hope i am clear. Please let me know how can i solve this.&lt;BR /&gt;regards&lt;BR /&gt;Hemant&lt;BR /&gt;&lt;/STRING.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Thu, 07 Sep 2006 10:37:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858510#M98400</guid>
      <dc:creator>Tnameh</dc:creator>
      <dc:date>2006-09-07T10:37:03Z</dc:date>
    </item>
    <item>
      <title>Re: strtok_r return value..............</title>
      <link>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858511#M98401</link>
      <description>OK, I see what you are trying to do now. Strtok skips past any adjacent tokens. You will need to either make your own function to parse it like you want to or use something like sscanf but that would not be a very safe choice.</description>
      <pubDate>Thu, 07 Sep 2006 12:06:29 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858511#M98401</guid>
      <dc:creator>A. Clay Stephenson</dc:creator>
      <dc:date>2006-09-07T12:06:29Z</dc:date>
    </item>
    <item>
      <title>Re: strtok_r return value..............</title>
      <link>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858512#M98402</link>
      <description>&lt;!--!*#--&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;what to you mean by that:&lt;BR /&gt;&amp;gt;&amp;gt;&lt;BR /&gt;What i need is when i parse string, i need sequential data,even strlen(pch) is zero.&lt;BR /&gt;&amp;lt;&amp;lt;&lt;BR /&gt;&lt;BR /&gt;Using this source&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STRING.H&gt;&lt;BR /&gt;&lt;BR /&gt;int main (int argc, char** argv)&lt;BR /&gt;{&lt;BR /&gt;char*str = argv[1];&lt;BR /&gt;&lt;BR /&gt;char * pch;&lt;BR /&gt;int i=0;&lt;BR /&gt;&lt;BR /&gt;if (argc &amp;lt;2) return(1);&lt;BR /&gt;printf ("Splitting string \"%s\" in tokens:\n",str);&lt;BR /&gt;pch = strtok (str,":");&lt;BR /&gt;/* so you get 0 if no string is found */&lt;BR /&gt;if (pch) i++;&lt;BR /&gt;printf("%d %s\n",i,str);&lt;BR /&gt;&lt;BR /&gt;if(!pch) return(0);&lt;BR /&gt;&lt;BR /&gt;while (pch = strtok(NULL,":"))&lt;BR /&gt;{&lt;BR /&gt;   i++;&lt;BR /&gt;   if(strlen(pch)) printf("%d %s\n",i,pch);&lt;BR /&gt;}&lt;BR /&gt;return 0;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;I do not get any empty fields:&lt;BR /&gt;./abc a:b  and&lt;BR /&gt;./abc a::b&lt;BR /&gt;will give the same output.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Is that what you want to differ?&lt;BR /&gt;&lt;BR /&gt;mfG Peter&lt;/STRING.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Thu, 07 Sep 2006 12:25:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858512#M98402</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2006-09-07T12:25:56Z</dc:date>
    </item>
    <item>
      <title>Re: strtok_r return value..............</title>
      <link>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858513#M98403</link>
      <description>&lt;!--!*#--&gt;Ok,&lt;BR /&gt;&lt;BR /&gt;just saw Clay's response - so if you want empty field information:&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STRING.H&gt;&lt;BR /&gt;&lt;BR /&gt;int main (int argc, char** argv)&lt;BR /&gt;{&lt;BR /&gt;char*str = argv[1];&lt;BR /&gt;&lt;BR /&gt;char *pch, *last;&lt;BR /&gt;int i=0,j,d,l;&lt;BR /&gt;&lt;BR /&gt;if (argc &amp;lt;2) return(1);&lt;BR /&gt;printf ("Splitting string \"%s\" in tokens:\n",str);&lt;BR /&gt;pch = strtok (str,":");&lt;BR /&gt;/* so you get 0 if no string is found */&lt;BR /&gt;if (pch) i++;&lt;BR /&gt;printf("%d %s\n",i,str);&lt;BR /&gt;&lt;BR /&gt;if(!pch) return(0);&lt;BR /&gt;&lt;BR /&gt;l = strlen(pch);&lt;BR /&gt;last = pch;&lt;BR /&gt;&lt;BR /&gt;while (pch = strtok(NULL,":"))&lt;BR /&gt;{&lt;BR /&gt;   d = pch - last;&lt;BR /&gt;   i++;&lt;BR /&gt;&lt;BR /&gt;/*&lt;BR /&gt;   if((d-l)&amp;gt;1) printf("%d empty fields\n",d-l-1);&lt;BR /&gt;*/&lt;BR /&gt;   for(j=d-l;j&amp;gt;1;j--) printf("%d\n",i++);&lt;BR /&gt;&lt;BR /&gt;   l=strlen(pch);&lt;BR /&gt;   if(l) printf("%d %s\n",i,pch);&lt;BR /&gt;   last=pch;&lt;BR /&gt;}&lt;BR /&gt;return 0;&lt;BR /&gt;&lt;/STRING.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Thu, 07 Sep 2006 12:41:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858513#M98403</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2006-09-07T12:41:14Z</dc:date>
    </item>
    <item>
      <title>Re: strtok_r return value, empty fields</title>
      <link>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858514#M98404</link>
      <description>&lt;!--  !*#  --&gt;&lt;P&gt;I guess mine is is very similar to Peter's.&lt;BR /&gt;&lt;BR /&gt;#include &amp;lt;stdio.h&amp;gt;&lt;BR /&gt;#include &amp;lt;string.h&amp;gt;&lt;BR /&gt;int main() {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; char buffer[80];&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static const char sep[] = ":";&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; char *brkt, *word, *save;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; strcpy(buffer, "This::abc:");&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; word = strtok_r(buffer, sep, &amp;amp;brkt);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf("word: %s\n",word);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; save = word + strlen(word)+1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; word = strtok_r(NULL, sep, &amp;amp;brkt);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /* dump out empties for each byte diff */&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (;save &amp;lt; word; ++save)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf("empty string\n");&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf("word: %s\n",word);&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;&lt;BR /&gt;}&lt;/P&gt;</description>
      <pubDate>Sun, 30 Oct 2011 01:22:51 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858514#M98404</guid>
      <dc:creator>Dennis Handly</dc:creator>
      <dc:date>2011-10-30T01:22:51Z</dc:date>
    </item>
    <item>
      <title>Re: strtok_r return value..............</title>
      <link>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858515#M98405</link>
      <description>Thanks a lot for all the replies, but what in case if i have string something like&lt;BR /&gt;"abc::::def::::"&lt;BR /&gt;&lt;BR /&gt;how to handle this situation. I guess all program will fail in this condition. &lt;BR /&gt;&lt;BR /&gt;any clue how to handle this.</description>
      <pubDate>Wed, 13 Sep 2006 08:38:32 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858515#M98405</guid>
      <dc:creator>Tnameh</dc:creator>
      <dc:date>2006-09-13T08:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: strtok_r return value..............</title>
      <link>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858516#M98406</link>
      <description>&lt;!--!*#--&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;dealing with trailing empty fields where left as an execise ...; (leading (multiple) empty fields can be done in a similar manner).&lt;BR /&gt;My solution stores the value of the initial string length and loops over the part skipped by strtok after its loop:&lt;BR /&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;STRING.H&gt;&lt;BR /&gt;&lt;BR /&gt;int main (int argc, char** argv)&lt;BR /&gt;{&lt;BR /&gt;char*str = argv[1];&lt;BR /&gt;&lt;BR /&gt;char *pch, *last;&lt;BR /&gt;int i=0,j,d,l,ll;&lt;BR /&gt;&lt;BR /&gt;if (argc &amp;lt;2) return(1);&lt;BR /&gt;printf ("Splitting string \"%s\" in tokens:\n",str);&lt;BR /&gt;/* to get traling empty fields: */&lt;BR /&gt;ll=strlen(str);&lt;BR /&gt;&lt;BR /&gt;pch = strtok (str,":");&lt;BR /&gt;/* so you get 0 if no string is found */&lt;BR /&gt;if (pch) i++;&lt;BR /&gt;printf("%d %s\n",i,str);&lt;BR /&gt;&lt;BR /&gt;if(!pch) return(0);&lt;BR /&gt;&lt;BR /&gt;l = strlen(pch);&lt;BR /&gt;last = pch;&lt;BR /&gt;&lt;BR /&gt;while (pch = strtok(NULL,":"))&lt;BR /&gt;{&lt;BR /&gt;   d = pch - last;&lt;BR /&gt;   i++;&lt;BR /&gt;&lt;BR /&gt;/*&lt;BR /&gt;   if((d-l)&amp;gt;1) printf("%d empty fields\n",d-l-1);&lt;BR /&gt;*/&lt;BR /&gt;   for(j=d-l;j&amp;gt;1;j--) printf("%d\n",i++);&lt;BR /&gt;&lt;BR /&gt;   l=strlen(pch);&lt;BR /&gt;   if(l) printf("%d %s\n",i,pch);&lt;BR /&gt;   last=pch;&lt;BR /&gt;}&lt;BR /&gt;/*&lt;BR /&gt;printf("trailing empty fields: %d\n", str+ll-last-l);&lt;BR /&gt;*/&lt;BR /&gt;for(j=0;j&lt;STR&gt;&lt;/STR&gt;return 0;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;mfG Peter&lt;/STRING.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Wed, 13 Sep 2006 09:30:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858516#M98406</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2006-09-13T09:30:18Z</dc:date>
    </item>
    <item>
      <title>Re: strtok_r return value..............</title>
      <link>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858517#M98407</link>
      <description>Hi Haihemant,&lt;BR /&gt;&lt;BR /&gt;As Clay pointed out:&lt;BR /&gt;&amp;gt;You will need to either make your own function to parse it like you want to or &amp;gt;use something like sscanf but that would not be a very safe choice.&lt;BR /&gt;&lt;BR /&gt;In case of variable-format strings your best bet will be to write your own function in order to parse/display individual fields as strtok will not be able to parse and display variable format strings that need to output NULL for zero length fields:&lt;BR /&gt;&lt;BR /&gt;"::abc::def" or "::abc::def:" or "abc::def" or "abc::def::"</description>
      <pubDate>Wed, 13 Sep 2006 16:38:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/strtok-r-return-value/m-p/3858517#M98407</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2006-09-13T16:38:03Z</dc:date>
    </item>
  </channel>
</rss>

