C: correct usage of strtok_r
Asked Answered
D

4

45

How can I use strtok_r instead of strtok to do this?

char *pchE = strtok(NULL, " ");

Now I'm trying to use strtok_r properly... But sometimes I get problems with the strtol. I have a thread that I execute 10 times (at the same time).

char *savedEndd1;
char *nomeClass = strtok_r(lineClasses, " ", &savedEndd1);
char *readLessonS = strtok_r (NULL, " ", &savedEndd1);
char *readNTurma = strtok_r(NULL, " ",  &savedEndd1);

if (readNTurma==NULL)
printf("CLASS STRTOL begin %s %s\n",nomeClass, readLessonS );
int numberNTurma = strtol(readNTurma, NULL, 10);

And I'm catching that readNTurma == NULL several times... Why is that? Cant understand why it comes NULL?

Dichogamy answered 12/4, 2013 at 0:41 Comment(1)
see this #2227698Timecard
B
53

The documentation for strtok_r is quite clear.

The strtok_r() function is a reentrant version strtok(). The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context between successive calls that parse the same string.

On the first call to strtok_r(), str should point to the string to be parsed, and the value of saveptr is ignored. In subsequent calls, str should be NULL, and saveptr should be unchanged since the previous call.

So you'd have code like

char str[] = "Hello world";
char *saveptr;
char *foo, *bar;

foo = strtok_r(str, " ", &saveptr);
bar = strtok_r(NULL, " ", &saveptr);
Bandy answered 12/4, 2013 at 0:47 Comment(0)
K
25

Tested example:

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

int main(void)
{
    char str[] = "1,22,333,4444,55555";
    char *rest = NULL;
    char *token;

    for (token = strtok_r(str, ",", &rest);
         token != NULL;
         token = strtok_r(NULL, ",", &rest)) {   
        printf("token:%s\n", token);
    }

    return 0;
}

Result.

token:1
token:22
token:333
token:4444
token:55555

Test: http://codepad.org/6xRdIecI

From linux documentation where emphasis is mine:

char *strtok_r(char *str, const char *delim, char **saveptr);

The strtok_r() function is a reentrant version strtok(). The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context between successive calls that parse the same string.

On the first call to strtok_r(), str should point to the string to be parsed, and the value of saveptr is ignored. In subsequent calls, str should be NULL, and saveptr should be unchanged since the previous call.

Different strings may be parsed concurrently using sequences of calls to strtok_r() that specify different saveptr arguments.

Ketubim answered 9/1, 2015 at 12:48 Comment(0)
G
11

I post a tested example to understand the correct usage of strtok_r() instead of using strtok() in nests.

first lets take a string "y.o.u,a.r.e,h.e.r.e" and separate using the delimiters "," and "."

using strtok()

#include<stdio.h>
#include<string.h>
int main(void) {

        char str[]="y.o.u,a.r.e,h.e.r.e";
        const char *p=",", *q=".";
        char *a,*b;

        for( a=strtok(str,p) ; a!=NULL ; a=strtok(NULL,p) ) {
                printf("%s\n",a);
                for( b=strtok(a,q) ; b!=NULL ; b=strtok(NULL,q) )
                        printf("%s\n",b);
        }

        return 0;
}

OUTPUT:

y.o.u
y
o
u

now lets use strtok_r() for same example

using strtok_r()

#include<stdio.h>
#include<string.h>
int main(void) {

        char str[]="y.o.u,a.r.e,h.e.r.e";
        const char *p=",",*q=".";
        char *a,*b,*c,*d;

        for( a=strtok_r(str,p,&c) ; a!=NULL ; a=strtok_r(NULL,p,&c) ) {
                printf("%s\n",a);

                for( b=strtok_r(a,q,&d) ; b!=NULL ; b=strtok_r(NULL,q,&d) )
                        printf("%s\n",b);
        }

        return 0;
}

OUTPUT:

y.o.u
y
o
u
a.r.e
a
r
e
h.e.r.e
h
e
r
e

therefore the strtok_r() has reentrant property whereas strtok() doesnot function like that.

Grow answered 11/5, 2019 at 8:20 Comment(0)
I
1
char str[]="string for sample";
char *reserve;
char *pchE = strtok_r(str, " ", &reserve);//when next call str -> NULL
Improvisatory answered 12/4, 2013 at 0:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.