博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
strtok函数真是个蹩脚而又恶心的设计(千万不要嵌套使用strtok函数)
阅读量:4139 次
发布时间:2019-05-25

本文共 3213 字,大约阅读时间需要 10 分钟。

       先来看一个程序:

#include 
#include
int main(){ char str[] = "a = 1\nb = 2\nc = 3\nd = 4\ne = 5\nf = 6"; char left[100] = {0}; char right[100] = {0}; char delims[] = "\n"; char *result = strtok(str, delims); while(result != NULL) { printf( "%s\n", result); sscanf(result, "%s = %s", left, right); printf( "%s:%s\n", left, right); result = strtok(NULL, delims); } return 0;}

     程序结果为:

a = 1

a:1
b = 2
b:2
c = 3
c:3
d = 4
d:4
e = 5
e:5
f = 6
f:6
  

    当然,下面这种情况就没法用sscanf来分割了(其实,需要对sscanf函数的返回值进行判断):

#include 
#include
int main(){ char str[] = "a=1\nb=2\nc=3\nd=4\ne=5\nf=6"; char left[100] = {0}; char right[100] = {0}; char delims[] = "\n"; char *result = strtok(str, delims); while(result != NULL) { printf( "%s\n", result); sscanf(result, "%s = %s", left, right); printf( "%s:%s\n", left, right); result = strtok(NULL, delims); } return 0;}

    所以,要想其他办法来解析,用strtok呗,程序如下:

#include 
#include
void splitIntoTwoParts(char *org, char sep, char *left, char *right, int n){ char delims[2] = {0}; delims[0] = sep; char *result = strtok(org, delims); strncpy(left, result, n - 1); while(result != NULL) { result = strtok(NULL, delims); strncpy(right, result, n - 1); return; }}int main(){ char str[] = "a=1"; char left[100] = {0}; char right[100] = {0}; splitIntoTwoParts(str, '=', left, right, 100); printf("%s\n", left); printf("%s\n", right); return 0;}

     结果为:

a

1

 

     可见,splitIntoTwoParts函数没什么问题(当然,为了简便起见,我没有考虑异常情况)

     strtok的诡异就在于,下面程序是有问题的:

#include 
#include
void splitIntoTwoParts(char *org, char sep, char *left, char *right, int n){ char delims[2] = {0}; delims[0] = sep; char *result = strtok(org, delims); strncpy(left, result, n - 1); while(result != NULL) { result = strtok(NULL, delims); strncpy(right, result, n - 1); return; }}int main(){ char str[] = "a=1\nb=2\nc=3\nd=4\ne=5\nf=6"; char left[100] = {0}; char right[100] = {0}; char delims[] = "\n"; char *result = strtok(str, delims); while(result != NULL) { printf( "%s\n", result); splitIntoTwoParts(result, '=', left, right, 100); printf( "%s:%s\n", left, right); result = strtok(NULL, delims); } return 0;}

     结果为:

a=1
a:1

    居然没有读到bcdef, 我晕,这个问题花了我1个小时,去百度搜一下就知道,你就明白了,原来,不能嵌套使用strtok函数。strtok好蹩脚啊。上述程序可以改为:

#include 
#include
void splitIntoTwoParts(char *org, char sep, char *left, char *right, int n){ char delims[2] = {0}; delims[0] = sep; char *result = strtok(org, delims); strncpy(left, result, n - 1); while(result != NULL) { result = strtok(NULL, delims); strncpy(right, result, n - 1); return; }}int main(){ char str[] = "a=1\nb=2\nc=3\nd=4\ne=5\nf=6"; char left[100] = {0}; char right[100] = {0}; char delims[] = "\n"; char *result = strtok(str, delims); char line[20][100] = {0}; int times = 0; while(result != NULL) { printf( "%s\n", result); strncpy(line[times++], result, 100 - 1); result = strtok(NULL, delims); } int i = 0; for(i = 0; i < times; i++) { splitIntoTwoParts(line[i], '=', left, right, 100); printf("%s:%s\n", left, right); } return 0;}

     结果为:

a=1
b=2
c=3
d=4
e=5
f=6
a:1
b:2
c:3
d:4
e:5
f:6

        注意,strtok函数还会引起原串的变化,真他妈的蹩脚的设计。

 

 

 

转载地址:http://esrvi.baihongyu.com/

你可能感兴趣的文章
将一个数插入到有序的数列中,插入后的数列仍然有序
查看>>
在有序的数列中查找某数,若该数在此数列中,则输出它所在的位置,否则输出no found
查看>>
万年历
查看>>
作为码农你希望面试官当场指出你错误么?有面试官这样遭到投诉!
查看>>
好多程序员都认为写ppt是很虚的技能,可事实真的是这样么?
查看>>
如果按照代码行数发薪水会怎样?码农:我能刷到公司破产!
查看>>
程序员失误造成服务停用3小时,只得到半月辞退补偿,发帖喊冤
查看>>
码农:很多人称我“技术”,感觉这是不尊重!纠正无果后果断辞职
查看>>
php程序员看过来,这老外是在吐糟你吗?看看你中了几点!
查看>>
为什么说程序员是“培训班出来的”就是鄙视呢?
查看>>
码农吐糟同事:写代码低调点不行么?空格回车键与你有仇吗?
查看>>
阿里p8程序员四年提交6000次代码的确有功,但一次错误让人唏嘘!
查看>>
一道技术问题引起的遐想,最后得出结论技术的本质是多么的朴实!
查看>>
985硕士:非科班自学编程感觉还不如培训班出来的,硕士白读了?
查看>>
你准备写代码到多少岁?程序员们是这么回答的!
查看>>
码农:和产品对一天需求,产品经理的需求是对完了,可我代码呢?
查看>>
程序员过年回家该怎么给亲戚朋友解释自己的职业?
查看>>
技术架构师的日常工作是什么?网友:搭框架,写公共方法?
查看>>
第四章 微信飞机大战
查看>>
九度:题目1008:最短路径问题
查看>>