[c]strtok_r,strtok_s, _strtok_s_l, wcstok_s, _wcstok_s_l, _mbstok_s, _mbstok_s_l

2016-10-11 写技术

On the first call to strtok_s the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok_s. Each call to strtok_s modifies strToken by inserting a null character after the token returned by that call. The context pointer keeps track of which string is being read and where in the string the next token is to be read. To read the next token from strToken, call strtok_s with a NULL value for the strToken argument, and pass the same context parameter. The NULL strToken argument causes strtok_s to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.

Since the context parameter supersedes the static buffers used in strtok and _strtok_l, it is possible to parse two strings simultaneously in the same thread.


Note:

strtok_r can used in linux.


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

char string[] = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
char *token = NULL;
char *next_token = NULL;
int main( void )
{
    token = strtok_r( string, seps, &next_token);
    while (token != NULL)
    {
        if (token != NULL)
        {
            printf( " %s\n", token );
            token = strtok_r( NULL, seps, &next_token);
        }
    }
}

标签: C

发表评论:

Powered by anycle 湘ICP备15001973号-1