strtok()函数
strtok()这个函数大家都应该碰到过,但好像总有些问题, 这里着重讲下它
首先看下MSDN上的解释:
char *strtok( char *strToken, const char *strDelimit );
Parameters
strToken
String containing token or tokens.
strDelimit
Set of delimiter characters.
Return Value
Returns 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.
Remarks
The strtok function finds the next token in strToken. The set of characters in strDelimitspecifies possible delimiters of the token to be found in strToken on the current call.
Security Note These functions incur a potential threat brought about by a buffer overrun problem. Buffer overrun problems are a frequent method of system attack, resulting in an unwarranted elevation of privilege. For more information, see Avoiding Buffer Overruns.
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 tostrtok 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 strTokenargument. 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.
Note Each function 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 functions 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.
很晕吧? 呵呵。。。
简单的说,就是函数返回第一个分隔符分隔的子串后,将第一参数设置为NULL,函数将返回剩下的子串。
下面我们来看一个例子:
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 ;
}
运行结果:
feng
ke
wei
说明:
函数strtok将字符串分解为一系列标记(token),标记就是一系列用分隔符(delimiting chracter,通常是空格或标点符号)分开的字符。注意,此的标记是由delim分割符分割的字符串喔。
例如,在一行文本中,每个单词可以作为标记,空格是分隔符。
需要多次调用strtok才能将字符串分解为标记(假设字符串中包含多个标记)。第一次调用strtok包含两个参数,即要标记化的字符串和包含用来分隔标记的字符的字符串(即分隔符):下列语句: tokenPtr = Strtok(string, " ")
将tokenPtr赋给string中第一个标记的指针。strtok的第二个参数””表示string中的标记用空格分开。
函数strtok搜索string中不是分隔符(空格)的第一个字符,这是第一个标记的开头。然后函数寻找字符串中的下一个分隔符,将其换成null(, w,)字符,这是当前标记的终点。注意标记的开始于结束。
函数strtok保存string中标记后面的下一个字符的指针,并返回当前标记的指针。
后面再调用strtok时,第一个参数为NULL,继续将string标记化。NULL参数表示调用strtok继续从string中上次调用 strtok时保存的位置开始标记化。
如果调用strtok时已经没有标记,则strtok返回NULL。注意strtok修改输入字符串,因此,如果调用strtok之后还要在程序中使用这个字符串,则应复制这个字 符串。
但如果用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
#include
#ifdef _MT
#include
#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[];
int count;
#ifdef _MT
_ptiddata ptd = _getptd();
#else /* _MT */
static char *nextoken; //保存剩余子串的静态变量
#endif /* _MT */
/* Clear control map */
for (count = ; count < ; count++)
map[count] = ;
/* Set bits in delimiter table */
do {
map[*ctrl >> ] |= ( << (*ctrl & ));
} 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 >> ] & ( << (*str & ))) && *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 >> ] & ( << (*str & )) ) {
*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;
}
原来, 该函数修改了原串.
所以,当使用char *test2 = "feng,ke,wei"作为第一个参数传入时,在位置①处, 由于test2指向的内容保存在文字常量区,该区的内容是不能修改的,所以会出现内存错误. 而char test1[] = "feng,ke,wei" 中的test1指向的内容是保存在栈区的,所以可以修改.
看到这里 大家应该会对文字常量区有个更加理性的认识吧.....
strtok()函数的更多相关文章
- shell脚本调用C语言之字符串切分之strtok函数
今天上午在写一个需求,要求的比较急,要求当天完成,我大致分析了一下,可以采用从shell脚本中插入一连串的日期,通过调用proc生成的可执行文件,将日期传入后台数据库,在数据库中进行计算.需要切分日期 ...
- strtok函数 分类: c++ 2014-11-02 15:24 214人阅读 评论(0) 收藏
strtok函数是cstring文件中的函数 strtok函数是cstring文件中的函数 其功能是截断字符串 原型为:char *strtok(char s[],const char *delin) ...
- strtok函数读写冲突问题
先上测试代码 #include "stdafx.h" #include <iostream> using namespace std; int _tmain(int a ...
- 字符串函数之Strtok()函数
Strtok()函数详解: 该函数包含在"string.h"头文件中 函数原型: char* strtok (char* str,constchar* delimiters ) ...
- STRTOK函数和STRTOK_R函数
STRTOK函数和STRTOK_R函数 注:本文转载自博客园,感谢作者整理! 1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct pe ...
- popen strtok 函数的使用
FILE * popen ( const char * command , const char * type ); int pclose ( FILE * stream ); type 参数只能 ...
- [转载]strtok函数和strtok_r函数
1.一个应用实例 网络上一个比较经典的例子是将字符串切分,存入结构体中.如,现有结构体 typedef struct person{ char name[25]; char sex[1 ...
- 温故而知新-strtok函数
温故而知新-strtok函数 记得之前没见过这个函数,是把字符串分割成更小的字符串 来个例子就是比较鲜明了 $string = "Hello world. Beautiful day tod ...
- 用strtok函数分割字符串
用strtok函数分割字符串 需要在loadrunner里面获得“15”(下面红色高亮的部分),并做成关联参数. //Body response 内容: <BODY><; PRE&g ...
- lr中用strtok函数分割字符串
需要在loadrunner里面获得“15”(下面红色高亮的部分),并做成关联参数. ,6,5,0,4,0,3,0,3,2,0,0,0,1 用web_reg_save_param取出“8,7,5,15, ...
随机推荐
- 使用ASP.NET MVC+Entity Framework快速搭建系统
详细资料: http://www.cnblogs.com/dingfangbo/p/5771741.html 学习 ASP.NET MVC 也有一段时间了,打算弄个小程序练练手,做为学习过程中的记录和 ...
- ref:LDAP入门
ref:https://www.jianshu.com/p/7e4d99f6baaf LDAP入门 首先要先理解什么是LDAP,当时我看了很多解释,也是云里雾里,弄不清楚.在这里给大家稍微捋一捋. 首 ...
- Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister报错解决办法
在做Hibernate框架数据库的关联关系映射练习中出现了Could not get constructor for org.hibernate.persister.entity.SingleTabl ...
- Bzoj1018/洛谷P4246 [SHOI2008]堵塞的交通(线段树分治+并查集)
题面 Bzoj 洛谷 题解 考虑用并查集维护图的连通性,接着用线段树分治对每个修改进行分治. 具体来说,就是用一个时间轴表示图的状态,用线段树维护,对于一条边,我们判断如果他的存在时间正好在这个区间内 ...
- MySQL 事物的隔离级别(简要)
事务的隔离级别 为什么 引入了 事务隔离级别?? 在数据库操作中,为了有效保证并发读取数据的正确性,提出的事务隔离级别. 更新丢失两个事务都同时更新一行数据,一个事务对数据的更新把另一个事务对数据的 ...
- alpha冲刺阶段博客集合
作业格式 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队) 团队名称: 那周余嘉熊掌将得队 作业目标:作业集合 团队信息: 队员学号 队员姓名 博客地址 备注 2216 ...
- Linux下使用thrfit
1.安装boost.thrfit 2.生成gen-cpp 3.编译其中的server,方法为: (1).直接使用g++编译 g++ -o server HelloWorld.cpp helloworl ...
- jQuery学习总结1
一.下载集CDN引入 1.1.官方下载 地址:http://jQuery.com/download/ jq自2.0版本开始,不再支持IE9一下浏览器:自3.0版本开始,针对移动端做了优化处理: 引入 ...
- 【线段树】【扫描线】Petrozavodsk Winter Training Camp 2018 Day 5: Grand Prix of Korea, Sunday, February 4, 2018 Problem A. Donut
题意:平面上n个点,每个点带有一个或正或负的权值,让你在平面上放一个内边长为2l,外边长为2r的正方形框,问你最大能圈出来的权值和是多少? 容易推出,能框到每个点的 框中心 的范围也是一个以该点为中心 ...
- 【SPFA】POJ1860-Currency Exchange
[题目大意] 给出每两种货币之间交换的手续费和汇率,求出从当前货币s开始交换,能否赚. [思路] 反向运用SPFA,判断是否有正环.每次队首元素出队之后,判断一下到源点s的距离是否增大,增大则返回tr ...