如何使"strtok函数"一次使用多个令牌字符串?函数指针会解决这个问题吗?

jdl*_*jdl 0 c c++

strtokfunction使用静态变量将字符串解析为标记.因此,当多次调用完成时,这会导致冲突.除了使用线程之外,我该如何执行以下操作:thx
- 我可以使用函数指针在2个不同的位置分配函数吗?这会使"strtok"中的静态变量分配到2个不同的位置吗?

//breaking up first by Sentence and than by Word.
char phrase[] = "My dog has fleas.\nAnd he gave them to me."; 
char del1[]   = "\n";
char del2[]   = " ";
char *token1;
char *token2;


token1 = strtok( phrase, del1);

while( token1 != NULL )
{
    printf("Sentence:  %s",token1);

    token2 = strtok( token1, del2);
    while( token2 != NULL ){
        token2 = strtok( NULL, del2);
        printf("WORD:  %s",token2);
    }

    token1 = strtok( NULL, del1);
}
Run Code Online (Sandbox Code Playgroud)

P.P*_*.P. 6

使用strtok_r()(可重入版本).

  • @jdl:这是一个公共域`strtok_r()`实现,应该可以在VC6中正常工作:http://snipplr.com/view/16918/strtokr/ (2认同)