`
bingtears
  • 浏览: 184890 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

strtok, wcstok, _mbstok

    博客分类:
  • C++C
阅读更多

Find the next token in a string.

char *strtok( char *strToken, const char *strDelimit );

wchar_t *wcstok( wchar_t *strToken, const wchar_t *strDelimit );

unsigned char *_mbstok( unsigned char*strToken, const unsigned char *strDelimit );

Routine Required Header Compatibility
strtok <string.h> ANSI, Win 95, Win NT
wcstok <string.h> or <wchar.h> ANSI, Win 95, Win NT
_mbstok <mbstring.h> Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

All of these functions return a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered.

Parameters

strToken

String containing token(s)

strDelimit

Set of delimiter characters

Remarks

The strtok function finds the next token in strToken. The set of characters in strDelimit specifies possible delimiters of the token to be found in strToken on the current call. wcstok and _mbstok are wide-character and multibyte-character versions of strtok. The arguments and return value of wcstok are wide-character strings; those of _mbstok are multibyte-character strings. These three functions behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tcstok strtok  _mbstok  wcstok 


On the first call to strtok, 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. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok 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.

Warning    Each of these functions uses a static variable for parsing the string into tokens. If multiple or simultaneous calls are made to the same function, a high potential for data corruption and inaccurate results exists. Therefore, do not attempt to call the same function simultaneously for different strings and be aware of calling one of these function from within a loop where another routine may be called that uses the same function.   However, calling this function simultaneously from multiple threads does not have undesirable effects.

Example

/* STRTOK.C: In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/

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

char string[] = "A string\tof ,,tokens\nand some   more tokens";
char seps[]    = " ,\t\n";
char *token;

void main( void )
{
    printf( "%s\n\nTokens:\n", string );
    /* Establish string and get the first token: */
    token = strtok( string, seps );
    while( token != NULL )
    {
       /* While there are tokens in "string" */
       printf( " %s\n", token );
       /* Get next token: */
       token = strtok( NULL, seps );
    }
}

Output

A string    of ,,tokens
and some   more tokens

Tokens:
A
string
of
tokens
and
some
more
tokens

分享到:
评论

相关推荐

    strtok的缺陷,使用strtok_s函数更新安全度

    strtok的缺陷,使用strtok_s函数更新安全度 #include "stdafx.h" #include "Windows.h" #include #include using namespace std; char string1[] = "A string\tof ,,tokens\nand some more tokens"; char ...

    Linux C编程一站式学习 25章习题_strtok & strtok_r

    1、出于练习的目的,strtok和strtok_r函数非常值得自己动手实现一遍,在这个过程中不仅可以更深刻地理解这两个函数的工作原理,也为以后理解“可重入”和“线程安全”这两个重要概念打下基础。 代码是自己实现的...

    strrchr strtok_r C库函数使用

    strrchr strtok_r C库函数使用

    C语言切割多层字符串(strtok_r strtok使用方法)

    主要介绍了C语言切割多层字符串的方法,说了strtok的弱点,使用strtok_r的方法

    strtok函数的用法 strtok函数的用法

    strtok函数的用法strtok函数的用法strtok函数的用法strtok函数的用法strtok函数的用法strtok函数的用法strtok函数的用法strtok函数的用法strtok函数的用法strtok函数的用法strtok函数的用法strtok函数的用法strtok...

    strtok 实现 原代码 strtok 实现 原代码

    strtok 实现 原代码strtok 实现 原代码strtok 实现 原代码strtok 实现 原代码strtok 实现 原代码strtok 实现 原代码strtok 实现 原代码strtok 实现 原代码strtok 实现 原代码strtok 实现 原代码

    C++中strtok()函数的用法介绍

    函数原型:char *strtok(char *s, const char *delim);Function:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。Description:strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串...

    strtok函数C实现

    char *strtok(const char *strToken, const char *strDelimit) 将strToken中以字符串strDelimit进行分割.vs2010测试通过

    C++ strtok应用方式浅析

    在C++编程语言中,对于字符的处理,可以通过使用C++ strtok来进行具体的操作。那么正确的应用方法我们将会在这篇文章中为大家详细介绍,希望能对大家有所帮助,提高实际程序开发效率。

    strtok使用範例

    strtok使用範例,可幫助C之初學者對程式學習上有些許幫助。 建議軟體VS 2005/2008

    C++常用字符串分割方法实例汇总

    本文实例汇总了C++常用字符串分割方法,分享给大家供大家参考。具体分析如下: 我们在编程的时候经常会碰到字符串...其它:strtok函数线程不安全,可以使用strtok_r替代。 示例: //借助strtok实现split #include &lt;

    自定义协议解析demo

    自定义协议解析demo,利用strtok_r对数据进行分割读取

    C语言strtok函数用法

    1、本文详细描述了c语言strtok函数的用法。 2、通过详细示例,让读者更直观地阅读,更清晰的理解。 3、示例代码可直接复制,编译后可直接运行。 4、根据示例以及运行结果,让读者加强记忆及理解。

    linux线程的实现 - aitao - 博客园1

    2. gdb 调试多线程 4. 三年回首:C基础 6. strsep和strtok_r替代strtok 7. 缓存穿透和缓存失效 8. mmap为什么比read

    strtok函数的用法大全

    函数原型:char *strtok(char *s, char *delim) 功能:作用于字符串s,以delim中的字符为分界符,将s切分成一个个子串;如果,s为空值NULL,则函数保存的指针SAVE_PTR在下一次调用中将作为起始位置。

    rtklib_2.4.2_p10.zip

    (8) replace strtok() by strtok_r() in expath() for thread-safe (9) fix problem on week rollover in RTCM 2 type 14 (10) fix problem on reading "C2" in RINEX 2.11 and 2.12 (11) fix bug on clock error ...

    strtok函数的使用示例

    strtok函数是字符串函数库中的一个函数,函数原型如下: char *strtok(char s[], const char *delim); 作用:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。 例如:”hello,hi:what?is!the....

    strtok.txt

    strtok.txt

    PHP strtok()函数的优点分析

    正因此,explode可以用中文切割,而strtok则不行,会乱码。 2、在使用while或for配合strtok()遍历时,可以随时更换分隔符,也可以随时用break跳出终止切割。 示例1:演示用中文+explode来切割 $string = “这是...

Global site tag (gtag.js) - Google Analytics