strtok函数真是个蹩脚而又恶心的设计(千万不要嵌套使用strtok函数)

972 篇文章 329 订阅
309 篇文章 11 订阅

       先来看一个程序:

#include <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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函数还会引起原串的变化,真他妈的蹩脚的设计。

 

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值