C语言strtok函数

2023-05-16

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,函数将返回剩下的子串。

例子:

#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变量中,保证了安全性。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C语言strtok函数 的相关文章

随机推荐

  • golang gorilla/mux设置静态目录

    发现网上都是类似下面的代码 96 96 96 s 61 34 Users golang golang 34 http Handle 34 static 34 http StripPrefix 34 static 34 http FileSe
  • ios系统removeCachedResponseForRequest无效的替代方案

    相信你能找到我这篇博客 xff0c 肯定是对URLCache缓存有了深刻的理解 xff0c 并且被ios系统api removeCachedResponse ForRequest使用起来并不能删除指定的缓存所困惑 其实也可以自己想办法来模拟
  • DHCPv6报文介绍

    摘自HUAWEI官网 DHCPv6报文格式如图11 2所示 图11 2 DHCPv6的报文格式 表11 1 DHCPv6报文中各个字段的含义 字段 长度 含义 msg type 1字节 表示报文的类型 xff0c 取值为1 xff5e 13
  • vnc服务器的搭建

    vnc服务的概述 xff1a VNC Virtual Network Computing 虚拟网络计算机的缩写 xff0c 主要是完成图形界面的远程控制使用 一个vnc系统是由客户端 服务器端和一个协议组成 服务器是分享其屏幕 xff0c
  • openwrt配置wifi桥接上级AP,再作为ap路由(可实现ip透传,例如远距离图像传输)

    第一步 上级ap配置为 接入点AP xff08 WDS xff09 xff0c 例如无人机的飞机端作为wds ap a xff0c 无线概况里点击修改 b xff0c ESSID改为你想要的名字 xff0c 要选择固定信道 xff08 非常
  • 菜鸟学Linux命令:ssh命令

    转载自品略图书馆 http www pinlue com article 2020 04 1003 1210139769049 html 1 查看SSH客户端版本 有的时候需要确认一下SSH客户端及其相应的版本号 使用ssh V命令可以得到
  • STM32串口发送数据

    串口通信经常作为开发调试的工具 xff0c 所以先介绍下串口通信 串口通讯 Serial Communication 是一种设备间非常常用的串行通讯方式 xff0c 因为它简单便捷 xff0c 大部分电子设备都支持该通讯方式 xff0c 电
  • npm ERR! code EINTEGRITY 解决方案

    报错信息 xff1a Error sha1 HsihLT8VutOkAReGpzpIZJY2twQ 61 integrity checksum failed when using sha1 wanted sha1 HsihLT8VutOkA
  • VScode搭建C/C++开发环境

    目录 1 VScode是什么 xff1f 2 VScode的下载和安装 2 1下载和安装 下载 xff1a 安装 xff1a 2 2环境的介绍 环境介绍 xff1a 安装中文版插件 xff1a 3 VScode配置C C 43 43 开发环
  • 从0开始跑通VINS FUSION(KITTI数据集)

    背景 xff1a VINS FUSION是香港科技大学在VINS MONO后做的推出的多功能版 xff0c 有双目的数据 xff0c 还有和GPS的融合 作为一个SLAM小白 xff0c 记录一下整个的跑通过程 代码连接 xff1a htt
  • ubuntu关于aptitude和apt-get

    起初GNU Linux系统中只有 tar gz 用户 必须自己编译他们想使用的每一个程序 在Debian出现之後 xff0c 人们认为有必要在系统 中添加一种机 制用来管理 安装在计算机上的软件包 人们将这套系统称为dpkg 至此着名的 p
  • C语言链表的简单编写

    代码分为3个部分 xff0c test c head h list c list c封装的函数 include 34 head h 34 创建一个空链表 Linklist list creat 申请一断空间 Linklist L L 61
  • java中a=a++;a=a+1;a+=1执行过程分析

    本文章内容前提是a数据类型为int 当a数据类型为int时 xff0c 执行a 61 a 43 43 后 xff0c a的数值不会变 xff1b 执行a 61 a 43 1后 xff0c 数值加1 xff1b 执行a 43 61 1后 xf
  • 【VPN(虚拟专用网)攻略大全】

    在 VPN 出现之前 xff0c 企业分支之间的数据传输只能依靠现有物理网络 xff08 例如 Internet xff09 由于 Internet 中存在多种不安全因素 xff0c 报文容易被网络中的黑客窃取或篡改 xff0c 最终造成数
  • Linux 如何检测硬盘坏道?

    在 Mac 和 Windows 下检测硬盘坏道有专门的工具 xff0c 或自带 或三方的都挺好用 xff0c 但是如何在 Linux 下检测硬盘坏道呢 xff1f 首先 xff0c 用 lsblk 命令查看下待检测硬盘的名字 xff1a 然
  • 图论-路径优化算法总结

    知乎主页 https www zhihu com people shuang shou cha dai 53 目录 1 xff1a Dijkstra算法 1 1 xff1a 算法思想 1 2 xff1a 算法步骤 1 3 xff1a 代码演
  • uORB发布订阅实例

    PX4SITL仿真 uORB实例 飞控串口读取外部传感器数据 xff1a 飞控开启一个进程读取外部传感器数据 xff0c 发布一个uORB主题 xff1b 另一个进程订阅前一个进程发布的主题 xff0c 订阅到的主题通过mavlink消息发
  • PX4仿真环境搭建

    PX4 SITL Simulation 前提准备 xff1a Ubuntu16 04 LTS 安装ROS kinetic 题外话 xff1a 如果连的是有IPV6的校园网 xff0c 在update时可能会访问IPV6地址出错 xff0c
  • PX4-Gazebo仿真学习笔记

    PX4 Gazebo仿真 xff1a http bbs amovauto com forum php mod 61 viewthread amp tid 61 486 amp extra 61 page 3D1 Simulator仿真器 x
  • C语言strtok函数

    1 strtok 语法 include lt string h gt char strtok char str const char delimiters 参数 xff1a str xff0c 待分割的字符串 xff08 c string