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函数 的相关文章

  • strtok()函数

    strtok 将字符串拆分成tokens xff0c tokens是被分隔符中的任何字符分隔的连续字符序列 char strtok char str const char sep sep参数是个字符串 xff0c 定义了用作分隔符的字符集合
  • c语言中strtok函数_在C语言中使用strtok()和strtok_r()函数

    c语言中strtok函数 In this article we ll take a look at using the strtok and strtok r functions in C 在本文中 xff0c 我们将介绍如何在C语言中使用
  • C/C++ strtok()是线程不安全

    最近发生一题 xff0c 通过使用hwasan发现了一个问题 xff0c 指示代码使用了非法内存 通过代码和dump一直查不到原因 xff0c 问题指示的是使用的内存被释放了 函数传入参数后 xff0c 马上使用按asprinf copy了
  • C语言:strtok()的用法。

    char strtok char str const char sep 1 sep参数是个字符串 xff0c 定义了用作分隔符的字符集合 xff1b 2 第一个参数指定一个字符串 xff0c 它包含了0个或者多个由sep字符串中一个或者多个
  • C++ strtok()无法截取连续两个分隔符之间的空字符串, 解决方法

    前言 问题描述 与前台约定按顺序解析对应信息 如果中间出现空数据 或者出现连续两个分隔符 strtok就会出问题 看下面这个例子 1 include lt string h gt 2 include lt stdio h gt 3 4 in
  • C语言strtok函数

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

    在 c 中 字符串分割函数主要有两种 一是strtok函数 另一个就是strsep函数 下面我们对这两个函数作一个详细解释说明 1 strtok 原形 char strtok char str const char delim 功能 分解字
  • 意外的 strtok() 行为

    我正在尝试使用 strtok 计算文件中的单词数 code c WHAT Use strtok to count the number of words in a file include
  • C++ strtok - 多次使用更多数据缓冲区

    我使用时没有什么问题strtok 功能 我正在解析两个文件 首先我将文件 1 加载到buffer 该文件包含我需要加载的第二个文件的名称 这两个文件都是逐行读取的 我的代码如下所示 char second file name 128 cha
  • 根据空格或“双引号字符串”将字符串解析为数组

    我试图获取用户输入字符串并解析为一个名为 char entire line 100 的数组 其中每个单词都放在数组的不同索引处 但如果字符串的一部分用引号封装 则应将其放在单个索引中 所以如果我有 char buffer 1024 0 fg
  • strtok 未处理的异常;写入位置访问冲突

    include
  • 使用strtok读取csv文件

    我正在尝试使用 C 中的 strtok 来读取 csv 文件 并将内容存储到 struct Game 的数组中 我的代码如下所示 FILE fp int i 0 if fp fopen Games csv r NULL printf Can
  • C 的 strtok() 和只读字符串文字

    char strtok c har s1 const char s2 重复调用此函数将字符串 s1 分解为 标记 即 字符串被分成子字符串 每个都以 0 结尾 其中 0 替换任何字符 包含在字符串 s2 中 第一次通话 使用要标记为 s1
  • strtok调用时出现问题

    我有一个像这样使用 strtok 的函数 void f1 char name char tmp tmp strtok names while tmp tmp strtok NULL 我有一个电话 f1 abc def 问题是在第一次调用中
  • 开发了 strtok 替代品

    我开发了自己的 strtok 版本 只是为了练习指针的使用 任何人都可以看到这有任何限制 或者无论如何我可以改进 void stvstrtok const char source char dest const char token Sea
  • C - 确定使用哪个分隔符 - strtok()

    假设我正在使用strtok 像这样 char token strtok input 有没有办法确定实际使用了哪个令牌 例如 如果输入类似于 Hello there How are you I m good End 我可以找出每个标记使用了哪
  • C 将输入文本文件解析为单词

    我正在尝试将输入文件 包含具有多行和分隔符的文本文档 即 解析为单词 我的函数 分割函数 是 int splitInput fp int i 0 char line 255 char array 5000 int x while fgets
  • 在C中提取两个特定字符串之间的字符串

    如何提取两个指定字符串之间的字符串 例如 有没有一种简单的方法可以使用它strtok 或者更简单的东西 编辑 两个指定的字符串是提取的字符串是Extract this 使用搜索第一个子字符串strstr 如果找到 则保存子字符串的数组索引
  • 如何在 Teradata 14 中对子字符串进行分组?

    我有下表天睿14 我不允许自己编写过程和函数 但我可以使用strtok strtok split to table etc id property 1 1234X Yel 2225Y Red 1234X Gre 2 3 1222Y Pin
  • strtok - 如何避免换行并放入字符串数组?

    如果我欺骗了主题 我真的很抱歉 我在这里搜索但没有结果 我有代码 void split char str char splitstr char p char splitbuf 32 int i 0 p strtok str while p

随机推荐

  • 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