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

相关文章

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境

Linux搭建ftp服务器的步骤

《Linux搭建ftp服务器的步骤》本文给大家分享Linux搭建ftp服务器的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录ftp搭建1:下载vsftpd工具2:下载客户端工具3:进入配置文件目录vsftpd.conf配置文件4:

Python Counter 函数使用案例

《PythonCounter函数使用案例》Counter是collections模块中的一个类,专门用于对可迭代对象中的元素进行计数,接下来通过本文给大家介绍PythonCounter函数使用案例... 目录一、Counter函数概述二、基本使用案例(一)列表元素计数(二)字符串字符计数(三)元组计数三、C

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键