char *strtok( char * strToken , const char * strDelimit );
函数用来从字符串中抽取想要的字段,首先看一个MSDN的例子:
//输出字符串中以指定分隔符隔开的字段
#include <string.h>
#include <stdio.h>
char string[] = "A string/tof ,,tokens/nand some more tokens";
char seps[] = " ,/t/n";
char *token;
void main( void )
{
printf( "%s/n/nTokens:/n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s/n", token );
// printf("after modify: %s/n",string);
/* Get next token: */
token = strtok( NULL, seps );
}
}
//Output
A string of ,,tokens
行 // printf("after modify: %s/n",string);前面的注释符。就会输出如下:
and some more tokens
Tokens:
A
string
of
tokens
and
some
more
tokens
如果去掉上面
A string of ,,tokens
after modify: A
and some more tokens
Tokens:
A
string
after modify: A
of
after modify: A
tokens
after modify: A
and
after modify: A
some
after modify: A
more
after modify: A
tokens
after modify: A
发现string已经修改了。(第一次调用strtok,在string里面的A后面加上了NULL字符,所以这样了。。。)
strtok第一次调用时,忽略开始的分隔符,返回第一个token的指针,并token后面加上null字符,记以null结尾。如果要得到剩下的字段,
可以继续调用strtok(),不过第一个参数设为NULL,第二个参数可根据你记录的需求来设定。因此,strtok函数可以提取多种分隔符分隔的字段。