strtok函数真是个蹩脚而又恶心的设计(千万不要嵌套使用strtok函数)
2013-11-02 00:43
435 查看
先来看一个程序:
程序结果为:
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函数的返回值进行判断):
所以,要想其他办法来解析,用strtok呗,程序如下:
结果为:
a
1
可见,splitIntoTwoParts函数没什么问题(当然,为了简便起见,我没有考虑异常情况)
strtok的诡异就在于,下面程序是有问题的:
结果为:
a=1
a:1
居然没有读到bcdef, 我晕,这个问题花了我1个小时,去百度搜一下就知道,你就明白了,原来,不能嵌套使用strtok函数。strtok好蹩脚啊。上述程序可以改为:
结果为:
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函数还会引起原串的变化,真他妈的蹩脚的设计。
#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函数还会引起原串的变化,真他妈的蹩脚的设计。
相关文章推荐
- 千万不要把 bool 设计成函数参数
- (转载)千万不要把bool设计成函数参数
- 编程规范---千万不要把 bool 设计成函数参数
- 千万不要把 bool 设计成函数参数
- 千万不要把 bool 设计成函数参数
- 千万不要把 bool 设计成函数参数
- 千万不要把 bool 设计成函数参数
- 千万不要把 bool 设计成函数参数(酷壳)
- 千万不要把 bool 设计成函数参数
- 不要在Lua中使用os.clock()函数
- 不要使用不理解或不适用的架构或设计模式
- 千万不要把 bool 当成函数参数
- 关于函数strtok和strtok_r的使用要点和实现原理(一)
- [Spark经验一]Spark RDD计算使用的函数里尽量不要使用全局变量
- 有效的使用和设计COM智能指针——条款20:安全的覆盖掉C++默默为我们编写的函数
- Ruby on Rails 中你使用了Kaminari 后,千万不要再引入will_pagination 这个Gem 了
- C语言中函数strtok的使用笔记
- 关于函数strtok和strtok_r的使用要点和实现原理(二)
- strtok()函数、fseek()函数、fwrite()函数、fread()函数的使用
- 线程函数的设计以及MsgWaitForMultipleObjects函数的使用要点