Returns a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered.
Remarks
The strtok function finds the next token in strToken. The set of characters in strDelimitspecifies possible delimiters of the token to be found in strToken on the current call.
Security Note These functions incur a potential threat brought about by a buffer overrun problem. Buffer overrun problems are a frequent method of system attack, resulting in an unwarranted elevation of privilege. For more information, see Avoiding Buffer
Overruns.
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 tostrtok 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 strTokenargument. 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.
Note Each function uses a static variable for parsing the string into tokens. If multiple or simultaneous calls are made to the same function, a high potential for data corruption and inaccurate results exists. Therefore, do not attempt to call the same function
simultaneously for different strings and be aware of calling one of these functions from within a loop where another routine may be called that uses the same function. However, calling this function simultaneously from multiple threads does not have undesirable
effects.
/* Initialize str. If string is NULL, set str to the saved
* pointer (i.e., continue breaking tokens out of the string
* from the last strtok call) */
if (string)
str = string; //第一次调用函数所用到的原串
else
#ifdef _MT
str = ptd->_token;
#else /* _MT */
str = nextoken; //将函数第一参数设置为NULL时调用的余串
#endif /* _MT */
/* Find beginning of token (skip over leading delimiters). Note that * there is no token iff this loop sets str to point to the terminal * null (*str == ‘\0‘) */ while ( (map[*str >> 3] & (1 << (*str & 7))) && *str ) str++;
string = str; //此时的string返回余串的执行结果 /* Find the end of the token. If it is not the end of the string, * put a null there. *///这里就是处理的核心了, 找到分隔符,并将其设置为‘\0‘,当然‘\0‘也将保存在返回的串中 for ( ; *str ; str++ )
if ( map[*str >> 3] & (1 << (*str & 7)) ) { *str++ = ‘\0‘; //这里就相当于修改了串的内容 ① break; } /* Update nextoken (or the corresponding field in the per-thread data
* structure */#ifdef