- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- strtok_r return value..............
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 02:04 AM
тАО09-07-2006 02:04 AM
I have string abc:def::::lkm. I am using strtok_r to find the substring and storing the value returned by it.
but strtok_r is not returning any value if their is nothing between delimiter.
#include
#include
int main()
{
char buffer[80], temp[80];
char *sep = ":";
char *brkt;
char *brkb;
char *word;
char *phrase;
strcpy(buffer, "This::abc:");
word = strtok_r(buffer, sep, &brkt);
printf("word : %s\n",word);
printf("brkt : %s\n",brkt);
word = strtok_r(NULL, sep, &brkt);
printf("word : %s\n",word);
printf("brkt : %s\n",brkt);
return 0;
}
Output is
$ ./abc
word : This
brkt : :abc:
word : abc
brkt :
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.
Hope i am clear.
Solved! Go to Solution.
- Tags:
- strtok
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 02:43 AM
тАО09-07-2006 02:43 AM
Re: strtok_r return value..............
You are really confusing a NULL pointer and a null string -- and they aren't the same thing at all.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 03:37 AM
тАО09-07-2006 03:37 AM
Re: strtok_r return value..............
Thanks for the reply.
#include
#include
int main ()
{
char str[] ="This:is::::::abc";
char * pch;
int i=0;
printf ("Splitting string \"%s\" in tokens:\n",str);
pch = strtok (str,":");
while (pch != NULL)
{
pch = strtok(NULL,":");
if( pch !=NULL) {
if(strlen(pch) > 0)
printf(" %d %s\n",i++,pch);
}
else
printf(" %d \n",i++);
}
return 0;
}
What i need is when i parse string, i need sequential data,even strlen(pch) is zero.
something like
1 This
2 is
3
4
5
6
7
8 abc
Hope i am clear. Please let me know how can i solve this.
regards
Hemant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 05:06 AM
тАО09-07-2006 05:06 AM
Re: strtok_r return value..............
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 05:25 AM
тАО09-07-2006 05:25 AM
Re: strtok_r return value..............
what to you mean by that:
>>
What i need is when i parse string, i need sequential data,even strlen(pch) is zero.
<<
Using this source
#include
#include
int main (int argc, char** argv)
{
char*str = argv[1];
char * pch;
int i=0;
if (argc <2) return(1);
printf ("Splitting string \"%s\" in tokens:\n",str);
pch = strtok (str,":");
/* so you get 0 if no string is found */
if (pch) i++;
printf("%d %s\n",i,str);
if(!pch) return(0);
while (pch = strtok(NULL,":"))
{
i++;
if(strlen(pch)) printf("%d %s\n",i,pch);
}
return 0;
}
I do not get any empty fields:
./abc a:b and
./abc a::b
will give the same output.
Is that what you want to differ?
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 05:41 AM
тАО09-07-2006 05:41 AM
Solutionjust saw Clay's response - so if you want empty field information:
#include
#include
int main (int argc, char** argv)
{
char*str = argv[1];
char *pch, *last;
int i=0,j,d,l;
if (argc <2) return(1);
printf ("Splitting string \"%s\" in tokens:\n",str);
pch = strtok (str,":");
/* so you get 0 if no string is found */
if (pch) i++;
printf("%d %s\n",i,str);
if(!pch) return(0);
l = strlen(pch);
last = pch;
while (pch = strtok(NULL,":"))
{
d = pch - last;
i++;
/*
if((d-l)>1) printf("%d empty fields\n",d-l-1);
*/
for(j=d-l;j>1;j--) printf("%d\n",i++);
l=strlen(pch);
if(l) printf("%d %s\n",i,pch);
last=pch;
}
return 0;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 08:59 PM - edited тАО10-29-2011 06:22 PM
тАО09-07-2006 08:59 PM - edited тАО10-29-2011 06:22 PM
Re: strtok_r return value, empty fields
I guess mine is is very similar to Peter's.
#include <stdio.h>
#include <string.h>
int main() {
char buffer[80];
static const char sep[] = ":";
char *brkt, *word, *save;
strcpy(buffer, "This::abc:");
word = strtok_r(buffer, sep, &brkt);
printf("word: %s\n",word);
save = word + strlen(word)+1;
word = strtok_r(NULL, sep, &brkt);
/* dump out empties for each byte diff */
for (;save < word; ++save)
printf("empty string\n");
printf("word: %s\n",word);
return 0;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2006 01:38 AM
тАО09-13-2006 01:38 AM
Re: strtok_r return value..............
"abc::::def::::"
how to handle this situation. I guess all program will fail in this condition.
any clue how to handle this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2006 02:30 AM
тАО09-13-2006 02:30 AM
Re: strtok_r return value..............
dealing with trailing empty fields where left as an execise ...; (leading (multiple) empty fields can be done in a similar manner).
My solution stores the value of the initial string length and loops over the part skipped by strtok after its loop:
#include
#include
int main (int argc, char** argv)
{
char*str = argv[1];
char *pch, *last;
int i=0,j,d,l,ll;
if (argc <2) return(1);
printf ("Splitting string \"%s\" in tokens:\n",str);
/* to get traling empty fields: */
ll=strlen(str);
pch = strtok (str,":");
/* so you get 0 if no string is found */
if (pch) i++;
printf("%d %s\n",i,str);
if(!pch) return(0);
l = strlen(pch);
last = pch;
while (pch = strtok(NULL,":"))
{
d = pch - last;
i++;
/*
if((d-l)>1) printf("%d empty fields\n",d-l-1);
*/
for(j=d-l;j>1;j--) printf("%d\n",i++);
l=strlen(pch);
if(l) printf("%d %s\n",i,pch);
last=pch;
}
/*
printf("trailing empty fields: %d\n", str+ll-last-l);
*/
for(j=0;j
}
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-13-2006 09:38 AM
тАО09-13-2006 09:38 AM
Re: strtok_r return value..............
As Clay pointed out:
>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.
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:
"::abc::def" or "::abc::def:" or "abc::def" or "abc::def::"