strtok用法--提取字符串

strtok用法--提取字符串

    最近看Petzold的《windows程序设计》,在Internet那章中看到如何在字符串中提取IP地址,特地标记一下:
1 GetDlgItemTextA (hwnd, wServer, szLabel, sizeof (szLabel));
2 strtok (szLabel, "(");
3 strcpy (szServer, strtok (NULL, ")"));
    在Msdn上查了一下,有下面一段:

    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.

Warning    Each of these functions 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 function 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.

    看来用了静态变量,还好有多线程的C运行库,否则在多线程在有麻烦了。

你可能感兴趣的:(strtok用法--提取字符串)