char *strtok(char *str, const char *delim)

会修改数据源。外部加锁才线程安全(strtok执行结束再解锁执行另一个strtok循环知道工作完成)

主要是以互斥访问strtok实现文件中的static外部变量char*old。源码如下。

#include <string.h>

static char *olds;

#undef strtok

/* Parse S into tokens separated by characters in DELIM.
If S is NULL, the last string strtok() was called with is
used. For example:
char s[] = "-abc-=-def";
x = strtok(s, "-"); // x = "abc"
x = strtok(NULL, "-="); // x = "def"
x = strtok(NULL, "="); // x = NULL
// s = "abc\0=-def\0"
*/
char *
strtok (s, delim)
char *s;
const char *delim;
{
char *token; if (s == NULL)
s = olds; /* Scan leading delimiters. */
s += strspn (s, delim);
if (*s == '\0')
{
olds = s;
return NULL;
} /* Find the end of the token. */
token = s;
s = strpbrk (token, delim);
if (s == NULL)
/* This token finishes the string. */
olds = __rawmemchr (token, '\0');
else
{
/* Terminate the token and make OLDS point past it. */
*s = '\0';
olds = s + ;
}
return token;
}

char *strsep(char **stringp, const char *delim)

会修改数据源。可重入的,注意这里虽然改动stringp的内容,主要是不在使用static静态变量了。

#include <string.h>

#undef __strsep
#undef strsep char *
__strsep (char **stringp, const char *delim)
{
char *begin, *end; begin = *stringp;
if (begin == NULL)
return NULL; /* A frequent case is when the delimiter string contains only one
character. Here we don't need to call the expensive `strpbrk'
function and instead work using `strchr'. */
if (delim[] == '\0' || delim[] == '\0')
{
char ch = delim[]; if (ch == '\0')
end = NULL;
else
{
if (*begin == ch)
end = begin;
else if (*begin == '\0')
end = NULL;
else
end = strchr (begin + , ch);
}
}
else
/* Find the end of the token. */
end = strpbrk (begin, delim); if (end)
{
/* Terminate the token and set *STRINGP past NUL character. */
*end++ = '\0';
*stringp = end;
}
else
/* No more delimiters; this is the last token. */
*stringp = NULL; return begin;
}

注意事项,会多次出现空字符串的问题,这是因为strsep在处理多于一个的delimit字符是会返回空字\0符串代替NULL(见源码便知),http://blog.csdn.net/striver1205/article/details/25601885

对比strtok,man手册提到:

The strsep() function was introduced as a replacement for strtok(3), since the latter cannot handle empty fields.
However, strtok(3) conforms to C89/C99 and hence is more portable.

stringp必须是二级指针,不能是指向字符数组的指针!

正确用法:

char a[]=......;char *p=a;strsep(&p,delim)

错误用法:

char a[]=...;strsep(&a,delim);会包参数类型错误

错误用法:

char *a=”xxxxx”;strsep(&a,delim)//错误,注意原串会被修改内容,字符常量会seg fault

char *strtok_r(char *str, const char *delim, char **saveptr)

会修改数据源。可重入,理由和strsep类似。

strsep和strtok_r替代strtok的更多相关文章

  1. 【C】——strtok()和strtok_r()

    下面的说明摘自于最新的Linux内核2.6.29,说明了strtok()这个函数已经不再使用,由速度更快的strsep()代替 /** linux/lib/string.c** Copyright ( ...

  2. C/C++ 字符串分割: strtok 与 strsep 函数说明

    函数原型: char *strtok(char *s, const char *delim); char *strsep(char **s, const char *delim); 功能:strtok ...

  3. 内存及字符串操作篇strlen strchar strcmp strcoll strcpy strdup strstr strtok strspn strrchr bcmp bcopy bzero index memccpy memset

    bcmp(比较内存内容) 相关函数 bcmp,strcasecmp,strcmp,strcoll,strncmp,strncasecmp 表头文件 #include<string.h> 定 ...

  4. C++常用字符串分割方法

    一.用strtok函数进行字符串分割 原型: char *strtok(char *str, const char *delim); 功能:分解字符串为一组字符串. 参数说明:str为要分解的字符串, ...

  5. C对字符串的部分操作

    字符串分割(C++)   经常碰到字符串分割的问题,这里总结下,也方便我以后使用. 一.用strtok函数进行字符串分割 原型: char *strtok(char *str, const char ...

  6. 字符串分割(C++)(转载)

    转载出自:http://www.cnblogs.com/MikeZhang/archive/2012/03/24/MySplitFunCPP.html 经常碰到字符串分割的问题,这里总结下,也方便我以 ...

  7. C++ 分割字符串两种方法

    原文地址:http://blog.csdn.net/xjw532881071/article/details/49154911 字符串切割的使用频率还是挺高的,string本身没有提供切割的方法,但可 ...

  8. 字符串分割(C++)

    一.用strtok函数进行字符串分割 原型: char *strtok(char *str, const char *delim); 功能:分解字符串为一组字符串. 参数说明:str为要分解的字符串, ...

  9. C++常用字符串分割方法(转)

    1.用strtok函数进行字符串分割 原型: char *strtok(char *str, const char *delim); 功能:分解字符串为一组字符串. 参数说明:str为要分解的字符串, ...

随机推荐

  1. Servlet中以HashMap存放临时变量,解决跳转新页面请求参数过多时浏览器地址栏超长

    具体使用方法如下: 1.在跳转之前将需要的参数串encodeURIComponent后作为参数value,UUID作为key一起POST到Servlet保存到HashMap中: 2.在Servlet发 ...

  2. Azure AD Connect 手动同步

    我们目前采用工具Azure AD Connect 目录同步工具将本地域控制器的用户信息同步至office365和Azure 在之前目录同步工具中使用Windows 任务计划程序或单独的 Windows ...

  3. Android Hook技术

    原文:http://blog.csdn.net/u011068702/article/details/53208825 附:Android Hook 全面入侵监听器 第一步.先爆项目demo照片,代码 ...

  4. ThreadPoolTimer -延迟执行, 周期执行

    介绍重新想象 Windows 8 Store Apps 之 线程池 通过 ThreadPoolTimer 实现延迟执行 通过 ThreadPoolTimer 实现周期执行 通过 ThreadPool ...

  5. 分享一种容易理解的js去重排序方法

    <script> var arr=[1,8,6,4,88,22,99,4,6,86,5,58,89,5]; //先使用sort()函数去重 var a=arr.sort(function ...

  6. Redis查询当前库有多少个 key

    info可以看到所有库的key数量 dbsize则是当前库key的数量 keys *这种数据量小还可以,大的时候可以直接搞死生产环境. dbsize和keys *统计的key数可能是不一样的,如果没记 ...

  7. 帝国cms内容页调用缩略图的原始尺寸图片

    在发布文章上传标题图片时,勾选"生成缩略图",将生成原图和对应的缩略图 原图的链接为[!--titlepic--]:/d/file/anlizhanshi/2016-11-25/8 ...

  8. maven管理本地jar包注意事项

    今天lucene中集成第三方中文分词器IKAnalyzer的时候遇到了相似的问题:lucene版本4.9.IKAnalyzer版本2012FF_hf1 直接去maven仓库下载,pom配置如下: &l ...

  9. jsp 头像上传显示部分代码实现

    <%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%> <% ...

  10. C++中有关数组的相关问题

    1.数组长度相关: strlen(from <string.h>)只是针对字符数组才有的,他不包含\0的长度.无法对其他类型求长度.sizeof()则可以对\0发起作用.记住(a.leng ...