C语言strtok函数
2016-04-14 17:08
417 查看
1.strtok
语法:
#include <string.h>
char *strtok( char *str, const char *delimiters );
参数:str,待分割的字符串(c-string);delimiters,分割符字符串。
该函数用来将字符串分割成一个个片段。参数str指向欲分割的字符串,参数delimiters则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delimiters中包涵的分割字符时,则会将该字符改为\0
字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。
CSDN中的解释
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,函数将返回剩下的子串。
例子:
2、strtok_s函数
strtok_s是windows下的一个分割字符串安全函数,其函数原型如下:
char *strtok_s( char *strToken, const char *strDelimit, char **buf);
这个函数将剩余的字符串存储在buf变量中,而不是静态变量中,从而保证了安全性。
3、strtok_r函数
strtok_s函数是linux下分割字符串的安全函数,函数声明如下:
char *strtok_r(char *str, const char *delim, char **saveptr);
该函数也会破坏带分解字符串的完整性,但是其将剩余的字符串保存在saveptr变量中,保证了安全性。
语法:
#include <string.h>
char *strtok( char *str, const char *delimiters );
参数:str,待分割的字符串(c-string);delimiters,分割符字符串。
该函数用来将字符串分割成一个个片段。参数str指向欲分割的字符串,参数delimiters则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delimiters中包涵的分割字符时,则会将该字符改为\0
字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。
CSDN中的解释
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,函数将返回剩下的子串。
例子:
#include<string.h> #include<stdio.h> int main() { char test[] = "feng,ke,wei"; char *p; p = strtok(test, ","); while(p) { printf("%s\n", p); p = strtok(NULL, ","); } return 0; }输出:
2、strtok_s函数
strtok_s是windows下的一个分割字符串安全函数,其函数原型如下:
char *strtok_s( char *strToken, const char *strDelimit, char **buf);
这个函数将剩余的字符串存储在buf变量中,而不是静态变量中,从而保证了安全性。
3、strtok_r函数
strtok_s函数是linux下分割字符串的安全函数,函数声明如下:
char *strtok_r(char *str, const char *delim, char **saveptr);
该函数也会破坏带分解字符串的完整性,但是其将剩余的字符串保存在saveptr变量中,保证了安全性。
相关文章推荐
- C++笔记2
- 《MFC 笔记》 Part3 c++的格式.hpp
- 一个c++封装的zookeeper库
- C/C++重要知识点汇总(持续更新中)
- 基于Leader/Follower模式的C++线程池
- 一些关于VC++开发的笔记
- 转]解析C语言中的sizeof
- C语言memcpy和memmove函数的理解和区别
- C++中map、set、hash_map、hash_set、unordered_map、unordered_set通俗辨析
- [hihoCoder 1149] 回文字符序列
- 搜素算法——二分搜索-1003
- C++/C源码分析
- C++中的虚函数、纯虚函数
- C语言中可变参数的用法
- cpp 并发编程小计
- C语言结构体大小
- C++学会STL
- 删除一个无头单链表的非尾节点(C语言)
- VS2010 C++ 操作Excel表格的编程实现
- C语言中#define的用法(转)