C++ strtok函数详解
文章目录
- 头文件
- 函数原型
- 参数
- 返回值
- 注意:
- 使用案例
- 拓展strtok_s与strtok_r
- strtok_s函数
- strtok_r函数
- 拓展用例:
strtok函数的作用是把字符串以规定的字符分割开:
头文件
#include <string.h>
函数原型
char *strtok(char s[], const char *delim);
参数
- str – 要被分解成一组小字符串的字符串。
- delim – 包含分隔符的 C 字符串。
返回值
该函数返回被分解的第一个子字符串,如果没有可检索的字符串,则返回一个空指针。
注意:
分割处理后原字符串 str 会变,变成第一个子字符串
strtok函数会把分割前的字符串破坏掉,即每次分割后,原来的字符串就会少掉一部分,完整性会被破坏。
例如:第一次分割之后,原字符串str是分割完成之后的第一个字符串,剩余的字符串存储在一个静态变量中,因此多线程同时访问该静态变量时,则会出现错误
使用案例
strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。如果要保持原字符串的完整,可以使用strchr和sscanf的组合等。
#include <string.h>
#include <stdio.h>int main () {char str[80] = "this is - CSDN - blog";const char s[2] = "-";char *token;/* 获取第一个子字符串 */token = strtok(str, s);/* 继续获取其他的子字符串 */while( token != NULL ) {printf( "%s\n", token );token = strtok(NULL, s);}return(0);
}
输出
this is CSDN blog
拓展strtok_s与strtok_r
strtok_s函数
strtok_s是windows下的一个分割字符串安全函数,其函数原型如下:
char *strtok_s( char *strToken, const char *strDelimit, char **buf);
这个函数将剩余的字符串存储在buf变量中,而不是静态变量中,从而保证了安全性。
strtok_r函数
strtok_s函数是linux下分割字符串的安全函数,函数声明如下:
char *strtok_r(char *str, const char *delim, char **saveptr);
该函数也会破坏待分解字符串的完整性,但是其将剩余的字符串保存在saveptr变量中,保证了安全性。
拓展用例:
linux C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> int main()
{ char str[]="ab,cd,ef"; char *ptr; char *p; printf("before strtok: str=%s\n",str); printf("begin:\n"); ptr = strtok_r(str, ",", &p); while(ptr != NULL){ // 输出时候 str都是 ab 也会被切割 破坏待分解字符串的完整性printf("str=%s\n",str); printf("ptr=%s\n",ptr); ptr = strtok_r(NULL, ",", &p); } return 0;
}
输入结果:
before strtok: str=ab,cd,ef
begin:
str=ab
ptr=ab
str=ab
ptr=cd
str=ab
ptr=ef
C++ strtok函数详解相关推荐
- C/C++根据特定字符分割字符串、读取文件去掉逗号等特定字符、strtok()函数详解
字符串分割情况 读取文件时,C++识别的是空格和换行符,但有时候文件是以符号分割的,如逗号等 字符串本身含有特殊符号,如逗号,@等 strtok()函数 strtok()函数能够按照特定的字符分解字符 ...
- strtok函数详解 看这一篇就够了-C语言(函数讲解、函数实现、使用用法举例、作用)
strtok()函数用于: 将字符串拆分为标记 函数介绍 函数声明:char * strtok ( char * str, const char * delimiters ); 头 文 件:#in ...
- c语言中strtok函数详解,手把手教你自主实现字符串切割函数,内附详细代码。
函数功能简介: 对字符串str进行切割,切割的标志为字符指针q指向的这两个字符 "# *":. 但是在对这个函数进行调用时,只有在第一次调用时,才会将str这个字符串的首地址传递过 ...
- strtok()函数详解
#include <string.h> #include <stdio.h> int main () {char str[80] = "This is-www.run ...
- 字符函数和字符串函数详解(二)strncpy strncat strncmp strstr strtok(及其模拟实现)
系列文章目录 字符函数和字符串函数详解(一)strlen strcpy strcat strcmp 字符函数和字符串函数详解(二)strncpy strncat strncmp strstr str ...
- C语言网络编程:accept函数详解
文章目录 前言 函数描述 代码实例 如何得到客户端的IP 和 端口号 前言 当使用tcp服务器使用socket创建通信文件描述符,bind绑定了文件描述符,服务器ip和端口号,listen将服务器端的 ...
- 【FFmpeg】函数详解(三)
FFmpeg函数详解 14.av_write_frame 15.av_interleaved_write_frame 16.av_write_trailer 17.avio_close 18.av_i ...
- 【FFmpeg】函数详解(二)
FFmpeg函数详解 9.av_dump_format 10.avio_open 11.avformat_write_header 12.avcodec_send_frame 13.avcodec_r ...
- 【FFmpeg】函数详解(一)
FFmpeg函数详解 一.错误码相关 1.AVERROR 2.av_strerror 3.其他错误码解释 二.编解码 1.获取编解码器 2.申请.释放上下文环境 3.打开编码器avcodec_open ...
- 【ES6】Generator函数详解
[ES6]Generator函数详解 一.Generator函数简介 基本概念 函数写法 yield关键字介绍 二.next方法的参数 三.for...of循环 四.关于普通throw()与Gener ...
最新文章
- 基于JDK1.8---HashMap源码分析
- C语言再学习 -- Xargs用法详解
- Spring整合mybatis中的sqlSession是如何做到线程隔离的?
- oracle sql语句 从指定条数查询
- Wix 安装部署(二)自定义安装界面和行为
- fopen文件路径怎么写_PHP文件上传
- 5折交叉验证_交叉验证的方法主要分为哪些?
- 《Arduino家居安全系统构建实战》——1.1 家居安全的基础设施
- html按钮样式_一组强大的CSS3 Material 按钮
- 腾讯“狠心”出手了,这款游戏,未成年人每天限玩1.5小时
- matlab2c使用c++实现matlab函数系列教程-rank函数
- MongoDB数据库设计中6条重要的经验法则,part 2
- turbo编译码c语言,Turbo码的编译码原理及仿真.pdf
- F2FS的sysfs调整文件系统参数
- 使用C++编写一个DHT爬虫,实现从DHT网络爬取BT种子
- 国密SM算法有哪些?
- pytorch_LSTM:参数
- upupoo视频使用Java代码下载
- UICC 之 USIM 详解全系列
- broker-list与bootstrap-server辨析