strtok用法--提取字符串
最近看Petzold的《windows程序設計》,在Internet那章中看到如何在字符串中提取IP地址,特地標記一下:2 strtok (szLabel, "(");
3 strcpy (szServer, strtok (NULL, ")"));
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運行庫,否則在多線程在有麻煩了。posted on 2009-10-19 23:05 gewala 閱讀(2343) 評論(0) 編輯 收藏 引用 所屬分類: C++