C++中关于strtok()函数的用法
strtok:
#include <string.h>
char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr);
功能:分解字符串为一组标记串。str为要分解的字符串,delim为分隔符字符串。
说明:首次调用时,str必须指向要分解的字符串,随后调用要把s设成NULL。
strtok在str中查找包含在delim中的字符并用NULL('/0')来替换,直到找遍整个字符串。
返回指向下一个标记串。当没有标记串时则返回空字符NULL。
实例:用strtok来判断ip地址是否合法:ip_strtok.c:
[c-sharp] view plaincopy
//ip_strtok.c
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char temp_buf[] = {};
char p_temp[];
char *p=NULL;
char *t = ".";
int m,n,i;
int j=,s=; if(argc!=)
{
printf("param must 2/n");
return -;
}
strcpy(temp_buf, argv[]);
for(i=; i<strlen(temp_buf);i++)
{
if(temp_buf[i] == *t)j++;
if(temp_buf[i] == *t && (temp_buf[i+]>=''&&temp_buf[i+]<=''))
{
s++;
}
}
if(j!= || j!=s)
{
printf("ip param format error/n");
return -;
}
p = strtok(temp_buf, t); while(p!=NULL)
{
strcpy(p_temp, p);
printf("%s /n", p_temp);
for(n=; n<strlen(p_temp);n++)
{
if(!(p_temp[n]>=''&&p_temp[n]<=''))
{
printf("ip param error/n");
return -;
}
} m = atoi(p_temp);
if(m>)
{
printf("ip invalid /n");
return -;
} p=strtok(NULL, ".");
printf("p = %s/n",p);
}
printf("ok! ip correct! ip=%s/n", argv[]);
return ;
}
编译运行:
[root@localhost liuxltest]# uname -r
2.6.
[root@localhost liuxltest]# gcc -Wall ip_strtok.c -o ip_strtok
[root@localhost liuxltest]# ./ip_strtok 172.18.4.255 p = p = p = p = (null)
ok! ip correct! ip=172.18.4.255
[root@localhost liuxltest]# ./ip_strtok 172.18.
ip param format error
strtok实现函数:
xl_strtok.c
[c-sharp] view plaincopy
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char *xl_strtok(char *str, const char *delim);
int main(int argc, char **argv)
{
if(argc != )
{
printf("param must be 3 /n");
return -;
} char *temp;
char *p_temp;
char *p;
temp=(char *)malloc();
p_temp=(char *)malloc();
strcpy(temp, argv[]);
strcpy(p_temp, argv[]);
printf("temp = %s /n",temp);
printf("p_temp = %s /n", p_temp);
p = xl_strtok(temp, p_temp);
printf("%s/n", p);
return ;
}
char *xl_strtok(char *s, const char *dm)
{
static char *last;
char *tok;
if(s == NULL)
s = last;
if(s == NULL)
return NULL;
tok = s;
while (*s != '/0')
{
while (*dm)
{
if (*s == *dm)
{
*s = '/0';
last = s + ;
break;
}
s++;
}
}
return tok;
}
strtok源码:
/*********************************************************************/
/***
*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 _SECURE_VERSION
#include <internal.h>
#else /* _SECURE_VERSION */
#include <mtdll.h>
#endif /* _SECURE_VERSION */
/***
*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.
*
*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:
*
*******************************************************************************/
#ifdef _SECURE_VERSION
#define _TOKEN *context
#else /* _SECURE_VERSION */
#define _TOKEN ptd->_token
#endif /* _SECURE_VERSION */
#ifdef _SECURE_VERSION
char * __cdecl strtok_s (
char * string,
const char * control,
char ** context
)
#else /* _SECURE_VERSION */
char * __cdecl strtok (
char * string,
const char * control
)
#endif /* _SECURE_VERSION */
{
unsigned char *str;
const unsigned char *ctrl = control;
unsigned char map[];
int count;
#ifdef _SECURE_VERSION
/* validation section */
_VALIDATE_RETURN(context != NULL, EINVAL, NULL);
_VALIDATE_RETURN(string != NULL || *context != NULL, EINVAL, NULL);
_VALIDATE_RETURN(control != NULL, EINVAL, NULL);
/* no static storage is needed for the secure version */
#else /* _SECURE_VERSION */
_ptiddata ptd = _getptd();
#endif /* _SECURE_VERSION */
/* 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
str = _TOKEN;
/* 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;
/* Find the end of the token. If it is not the end of the string,
* put a null there. */
for ( ; *str ; str++ )
if ( map[*str >> ] & ( << (*str & )) ) {
*str++ = '/0';
break;
}
/* Update nextoken (or the corresponding field in the per-thread data
* structure */
_TOKEN = str;
/* Determine if a token has been found. */
if ( string == str )
return NULL;
else
return string;
}
C++中关于strtok()函数的用法的更多相关文章
- C语言中关于scanf函数的用法
scanf()函数的控制串 函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]); scanf()函数是通用终端格式化 ...
- JavaScript中字符串分割函数split用法实例
这篇文章主要介绍了JavaScript中字符串分割函数split用法,实例分析了javascript中split函数操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了JavaSc ...
- C中的时间函数的用法
C中的时间函数的用法 这个类展示了C语言中的时间函数的常用的用法. 源代码: #include <ctime>#include <iostream> using name ...
- (转)Python中的split()函数的用法
Python中的split()函数的用法 原文:https://www.cnblogs.com/hjhsysu/p/5700347.html Python中有split()和os.path.split ...
- 数据库SQL中case when函数的用法
Case具有两种格式,简单Case函数和Case搜索函数.这两种方式,可以实现相同的功能.简单Case函数的写法相对比较简洁,但是和Case搜索函数相比,功能方面会有些限制,比如写判断式. 简单Cas ...
- mysql中的group_concat函数的用法
本文通过实例介绍了MySQL中的group_concat函数的使用方法,比如select group_concat(name) . MySQL中group_concat函数 完整的语法如下: grou ...
- Java中的split函数的用法
Java中的 split 函数是用于按指定字符(串)或正则去分割某个字符串,结果以字符串数组形式返回: 例如: String str="1234@abc"; String[] a ...
- PHP中日期时间函数date()用法总结
date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考. 格式化日期date() 函数的第一个参数规定了如何格式化日期/时间.它使用字母 ...
- Python中的join()函数的用法
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字 ...
随机推荐
- hibernate 操作 Postgresql 数据库报 operator does not exist: integer = character varying
网上的说法如下: Java开发Postgresql 数据库兼容应用的问题,与Oracle有一些不同: Java类型映射数据库类型的不同,Oracle jdbc驱动程序处理Java String类型可正 ...
- Android:日常学习笔记(6)——探究活动(4)
Android:日常学习笔记(6)——探究活动(4) 活动的启动模式 standard模式 standard是活动默认的启动模式,在不进行显示定义的情况下,所有活动都会自动使用这种启动模式. stan ...
- 06 Spring框架 依赖注入(三)多配置文件
在Spring前几节的学习中我们都使用了一个配置文件,就像struts2中可以包含其他的配置文件,我们能不能使用多个配置文件呢(在工程比庞大,配置比较多的时候)? Spring多配置文件分为两种: 平 ...
- 【LeetCode】【定制版排序】Sort Colors
之前转载过一篇STL的sort方法底层详解的博客:https://www.cnblogs.com/ygh1229/articles/9806398.html 但是我们在开发中会根据自己特定的应用,有新 ...
- 配置树莓派3和局域网NTP服务器实现内网时间校准
一.配置局域网NTP服务器 1.安装ntp-4.2.8p5-win32-setup.exe 下载地址:https://www.meinbergglobal.com/english/sw/ntp.htm ...
- github使用——如何恢复被删去文件。
首先git删除文件包括以下几种情况 删除本地文件,但是未添加到暂存区: 删除本地文件,并且把删除操作添加到了暂存区: 把暂存区的操作提交到了本地git库: 把本地git库的删除记录推送到了远程服务器g ...
- Python编程-模块和包
一.模块 1.什么是模块? 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 2.为何要使用模块? 如果你退出python解释器然后重新进入,那么你之前定义的函 ...
- NoSQL数据库memcache和redis区别
在web后台发开面试中,经常会被问道memcache和redis的区别和使用情况. 其中memcache和redis都是基于内存存储的缓存系统,存储形式key--value键值对的形式. 区别: 1. ...
- python补充知识点
1. 在python2中用xrange,在python3中直接使用range就好了 2. 常数 None在逻辑判断的时候指代False,其他方式不代表True或者False 3. for循环只作用域容 ...
- seajs 入门
最近想搞搞JS模块化, 读到了园子里的一篇好文: http://www.cnblogs.com/lvdabao/p/js-modules-develop.html 看里面讲seajs不错, 于是想学 ...