先来看一个程序:
#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函数还会引起原串的变化,真他妈的蹩脚的设计。