strtok用起来的结果怎么跟帮助上的不一样?
帮助文档上说如果strtok找不到分隔符分隔的子串就会返回NULL,但是我发现它会返回整个源串,这是怎么一回事?
#include <stdio.h>
#include <string.h>
void main()
{
char Src[]="Msg=War,Time=Now";
char *s="";
s = strtok(Src,";"); //找";"
printf("%s\n",s);
}
结果返回"Msg=War,Time=Now"!!!
问题点数:30、回复次数:3Top
1 楼systemid(早睡早起身体好)回复于 2002-05-23 21:09:54 得分 0
On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.Top
2 楼steedhorse(晨星)回复于 2002-05-23 21:13:05 得分 15
第一次应该返回整个串,MSDN里说:
They return NULL when no more tokens are found.
下一次应该返回空。Top
3 楼dgj(我是一匹狼)回复于 2002-05-23 21:53:06 得分 15
返回整个串很正常啊,没有分割符分割他那不就是整个串就是一部分了吗?Top
4 楼Sachow(SC)回复于 2002-05-24 09:42:04 得分 0
这样理解倒是也有道理,但不是我要的结果,我希望第一次找不到就返回NULL,只好自己多写点代码了……Top





