Linux C编程——strtok_r 和 strtok 函数

2024-06-03 22:18
文章标签 linux 函数 编程 strtok

本文主要是介绍Linux C编程——strtok_r 和 strtok 函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

 

一、简介

 

NAMEstrtok, strtok_r - extract tokens from stringsSYNOPSIS#include <string.h>char *strtok(char *str, const char *delim);char *strtok_r(char *str, const char *delim, char **saveptr);DESCRIPTIONThe strtok() function parses a string into a sequence of tokens.  On the first call to strtok() the string to be parsed should be specified in str.  In each subsequent call that should parse the same  string,  str should be NULL.解析str指向的字符串,delim指向分隔符,后续调用解析相同的字符串时,str设置为NULL。The delim argument specifies a set of characters that delimit the tokens in the parsed string.  The caller may specify different strings in delim in successive calls that parse the same string.后续解析相同字符串时,分隔符delim可以不同。Each call to strtok() returns a pointer to a null-terminated  string  containing  the  next  token.   This  string does not include the delimiting character.  If no more tokens are found, strtok() returns NULL.strtok 返回以NULL结尾的指向后续字符的指针,指针指向的字符串不包含下一个分隔符delim及其后面的字符。解析到结尾时,返回NULL。A  sequence of two or more contiguous delimiter characters in the parsed string is considered to be a single delimiter.  Delimiter characters at the start or end of the string are ignored.  Put another way:  the tokens returned by strtok() are always non-empty strings.待解析字符串str中连续的多个delim被认为只有一个,str中开头和结尾的delim被忽略,运行例子如下:[root@localhost tmp]# ./a.out "||a,b,c||e,d,f|h,i,j|w,z,d,e" "|" ','  1: a,b,c --> a --> b --> c2: e,d,f --> e --> d --> f3: h,i,j --> h --> i --> j4: w,z,d,e --> w --> z --> d --> eThe  strtok_r()  function  is a reentrant version strtok().  The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context between successive calls  that parse the same string.strtok_r是strtok的可重入版。On  the first call to strtok_r(), str should point to the string to be parsed, and the value of saveptr is ignored.  In subsequent calls, str should be NULL, and saveptr should  be  unchanged  since  the  previous call.首次调用strtok_r时,str设置为待解析字符串,saveptr 被忽略,后续调用str设为NULL,saveptr 不变,用来连接上次的解析。Different strings may be parsed concurrently using sequences of calls to strtok_r() that specify different saveptr arguments.如例子中所示,saveptr 不同,表示解析不同的字符串。

 

二、举例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>int
main(int argc, char *argv[])
{
char *str1, *str2, *token, *subtoken;
char *saveptr1, *saveptr2;
int j;if (argc != 4) {fprintf(stderr, "Usage: %s string delim subdelim\n",argv[0]);exit(EXIT_FAILURE);
}for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {token = strtok_r(str1, argv[2], &saveptr1);if (token == NULL)break;printf("%d: %s", j, token);for (str2 = token; ; str2 = NULL) {subtoken = strtok_r(str2, argv[3], &saveptr2);if (subtoken == NULL)break;printf(" --> %s", subtoken);}puts("");
}exit(EXIT_SUCCESS);
} /* main */

测试结果:

[root@localhost tmp]# gcc strtok_r.c                             
[root@localhost tmp]# ./a.out "a,b,c|e,d,f|h,i,j|w,z,d,e" '|' ','
1: a,b,c --> a --> b --> c
2: e,d,f --> e --> d --> f
3: h,i,j --> h --> i --> j
4: w,z,d,e --> w --> z --> d --> e[root@localhost tmp]# ./a.out "||a,b,c||e,d,f|h,i,j|w,z,d,e" "|" ','  
1: a,b,c --> a --> b --> c
2: e,d,f --> e --> d --> f
3: h,i,j --> h --> i --> j
4: w,z,d,e --> w --> z --> d --> e

 

这篇关于Linux C编程——strtok_r 和 strtok 函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1028266

相关文章

Linux内核定时器使用及说明

《Linux内核定时器使用及说明》文章详细介绍了Linux内核定时器的特性、核心数据结构、时间相关转换函数以及操作API,通过示例展示了如何编写和使用定时器,包括按键消抖的应用... 目录1.linux内核定时器特征2.Linux内核定时器核心数据结构3.Linux内核时间相关转换函数4.Linux内核定时

Linux镜像文件制作方式

《Linux镜像文件制作方式》本文介绍了Linux镜像文件制作的过程,包括确定磁盘空间布局、制作空白镜像文件、分区与格式化、复制引导分区和其他分区... 目录1.确定磁盘空间布局2.制作空白镜像文件3.分区与格式化1) 分区2) 格式化4.复制引导分区5.复制其它分区1) 挂载2) 复制bootfs分区3)

pandas使用apply函数给表格同时添加多列

《pandas使用apply函数给表格同时添加多列》本文介绍了利用Pandas的apply函数在DataFrame中同时添加多列,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录一、Pandas使用apply函数给表格同时添加多列二、应用示例一、Pandas使用apply函

Linux服务器数据盘移除并重新挂载的全过程

《Linux服务器数据盘移除并重新挂载的全过程》:本文主要介绍在Linux服务器上移除并重新挂载数据盘的整个过程,分为三大步:卸载文件系统、分离磁盘和重新挂载,每一步都有详细的步骤和注意事项,确保... 目录引言第一步:卸载文件系统第二步:分离磁盘第三步:重新挂载引言在 linux 服务器上移除并重新挂p

Python中Namespace()函数详解

《Python中Namespace()函数详解》Namespace是argparse模块提供的一个类,用于创建命名空间对象,它允许通过点操作符访问数据,比字典更易读,在深度学习项目中常用于加载配置、命... 目录1. 为什么使用 Namespace?2. Namespace 的本质是什么?3. Namesp

Linux下屏幕亮度的调节方式

《Linux下屏幕亮度的调节方式》文章介绍了Linux下屏幕亮度调节的几种方法,包括图形界面、手动调节(使用ACPI内核模块)和外接显示屏调节,以及自动调节软件(CaliseRedshift和Reds... 目录1 概述2 手动调节http://www.chinasem.cn2.1 手动屏幕调节2.2 外接显

Linux(centos7)虚拟机没有IP问题及解决方案

《Linux(centos7)虚拟机没有IP问题及解决方案》文章介绍了在CentOS7中配置虚拟机网络并使用Xshell连接虚拟机的步骤,首先,检查并配置网卡ens33的ONBOOT属性为yes,然后... 目录输入查看ZFhrxIP命令:ip addr查看,没有虚拟机IP修改ens33配置文件重启网络Xh

linux实现对.jar文件的配置文件进行修改

《linux实现对.jar文件的配置文件进行修改》文章讲述了如何使用Linux系统修改.jar文件的配置文件,包括进入文件夹、编辑文件、保存并退出编辑器,以及重新启动项目... 目录linux对.jar文件的配置文件进行修改第一步第二步 第三步第四步总结linux对.jar文件的配置文件进行修改第一步进

MySQL中如何求平均值常见实例(AVG函数详解)

《MySQL中如何求平均值常见实例(AVG函数详解)》MySQLavg()是一个聚合函数,用于返回各种记录中表达式的平均值,:本文主要介绍MySQL中用AVG函数如何求平均值的相关资料,文中通过代... 目录前言一、基本语法二、示例讲解1. 计算全表平均分2. 计算某门课程的平均分(例如:Math)三、结合

linux ssh如何实现增加访问端口

《linuxssh如何实现增加访问端口》Linux中SSH默认使用22端口,为了增强安全性或满足特定需求,可以通过修改SSH配置来增加或更改SSH访问端口,具体步骤包括修改SSH配置文件、增加或修改... 目录1. 修改 SSH 配置文件2. 增加或修改端口3. 保存并退出编辑器4. 更新防火墙规则使用uf