今天用这个函数用的郁闷了好一会儿,就是因为字符串是用的test2指针形式,而没有用test1那样的数组形式!
下面是我在网上看到的:
int main() {
char test1[] = "feng,ke,wei";
char *test2 = "feng,ke,wei";
char *p; p = strtok(test1, ",");
while(p)
{
printf("%s\n", p);
p = strtok(NULL, ",");
}
return 0;
}
运行结果:
feng
ke
wei
但如果用p = strtok(test2, ",")则会出现内存错误,这是为什么呢?是不是跟它里面那个静态变量有关呢? 我们来看看它的原码:
/***
*strtok.c - tokenize a string with given delimiters
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines strtok() - breaks string into series of token
* via repeated calls.
*
*******************************************************************************/
#include <cruntime.h>
#include <string.h>
#ifdef _MT
#include <mtdll.h>
#endif /* _MT */
/***
*char *strtok(string, control) - tokenize string with delimiter in control
*
*Purpose:
* strtok considers the string to consist of a sequence of zero or more
* text tokens separated by spans of one or more control chars. the first
* call, with string specified, returns a pointer to the first char of the
* first token, and will write a null char into string immediately
* following the returned token. subsequent calls with zero for the first
* argument (string) will work thru the string until no tokens remain. the
* control string may be different from call to call. when no tokens remain
* in string a NULL pointer is returned. remember the control chars with a
* bit map, one bit per ascii char. the null char is always a control char.
* //这里已经说得很详细了!!比MSDN都好!
*Entry:
* char *string - string to tokenize, or NULL to get next token
* char *control - string of characters to use as delimiters
*
*Exit:
* returns pointer to first token in string, or if string
* was NULL, to next token
* returns NULL when no more tokens remain.
*
*Uses:
*
*Exceptions:
*
*******************************************************************************/
char * __cdecl strtok (
char * string,
const char * control
)
{
unsigned char *str;
const unsigned char *ctrl = control;
unsigned char map[32];
int count;
#ifdef _MT
_ptiddata ptd = _getptd();
#else /* _MT */
static char *nextoken; //保存剩余子串的静态变量
#endif /* _MT */
/* Clear control map */
for (count = 0; count < 32; count++)
map[count] = 0;
/* Set bits in delimiter table */
do {
map[*ctrl >> 3] |= (1 << (*ctrl & 7));
} while (*ctrl++);
/* Initialize str. If string is NULL, set str to the saved
* pointer (i.e., continue breaking tokens out of the string
* from the last strtok call) */
if (string)
str = string; //第一次调用函数所用到的原串
else
#ifdef _MT
str = ptd->_token;
#else /* _MT */
str = nextoken; //将函数第一参数设置为NULL时调用的余串
#endif /* _MT */
/* Find beginning of token (skip over leading delimiters). Note that
* there is no token iff this loop sets str to point to the terminal
* null (*str == '\0') */
while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
str++;
string = str; //此时的string返回余串的执行结果
/* Find the end of the token. If it is not the end of the string,
* put a null there. */
//这里就是处理的核心了, 找到分隔符,并将其设置为'\0',当然'\0'也将保存在返回的串中
for ( ; *str ; str++ )
if ( map[*str >> 3] & (1 << (*str & 7)) ) {
*str++ = '\0'; //这里就相当于修改了串的内容 ①
break;
}
/* Update nextoken (or the corresponding field in the per-thread data
* structure */
#ifdef _MT
ptd->_token = str;
#else /* _MT */
nextoken = str; //将余串保存在静态变量中,以便下次调用
#endif /* _MT */
/* Determine if a token has been found. */
if ( string == str )
return NULL;
else
return string;
分享到:
相关推荐
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_s函数更新安全度 #include "stdafx.h" #include "Windows.h" #include #include using namespace std; char string1[] = "A string\tof ,,tokens\nand some more tokens"; char ...
char *strtok(const char *strToken, const char *strDelimit) 将strToken中以字符串strDelimit进行分割.vs2010测试通过
1、出于练习的目的,strtok和strtok_r函数非常值得自己动手实现一遍,在这个过程中不仅可以更深刻地理解这两个函数的工作原理,也为以后理解“可重入”和“线程安全”这两个重要概念打下基础。 代码是自己实现的...
在C++编程语言中,对于字符的处理,可以通过使用C++ strtok来进行具体的操作。那么正确的应用方法我们将会在这篇文章中为大家详细介绍,希望能对大家有所帮助,提高实际程序开发效率。
strtok使用範例,可幫助C之初學者對程式學習上有些許幫助。 建議軟體VS 2005/2008
strrchr strtok_r C库函数使用
1、本文详细描述了c语言strtok函数的用法。 2、通过详细示例,让读者更直观地阅读,更清晰的理解。 3、示例代码可直接复制,编译后可直接运行。 4、根据示例以及运行结果,让读者加强记忆及理解。
函数原型:char *strtok(char *s, char *delim) 功能:作用于字符串s,以delim中的字符为分界符,将s切分成一个个子串;如果,s为空值NULL,则函数保存的指针SAVE_PTR在下一次调用中将作为起始位置。
主要介绍了C语言切割多层字符串的方法,说了strtok的弱点,使用strtok_r的方法
strtok.txt
strtok函数是字符串函数库中的一个函数,函数原型如下: char *strtok(char s[], const char *delim); 作用:分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。 例如:”hello,hi:what?is!the....
标记解析(`Tokenizing`)是最简单也是最常见的解析问题,也就是根据分隔符把一个字符串分割为几个部分。这个定义覆盖了所有这种类型的任务。根据空白分隔符(例如`" \t\n\r"`之一)分割单词。假设有个像`"/usr/...
1、可以一次定义多个分隔符。函数在执行时,是按单个分隔符来切割,而不是按整个分隔符,而explode则是按整个分隔串来切割的。正因此,explode可以用中文切割,而strtok则不行,会乱码。 2、在使用while或for配合...
Description:strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串,当strtok()在参数s的字符串中发现到参数delim的分割字符时 则会将该字符改为\0 字符。在第一次调用时,...
原字符串中包含浮点数、逗号、字符串、整数,长整型数等,本程序实现的是取出其中的浮点数和长整型数