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运行库,否则在多线程在有麻烦了。

posted on 2009-10-19 23:05 gewala 阅读(2313) 评论(0)  编辑 收藏 引用 所属分类: C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


<2009年10月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

设计模式 网络编程

网络

搜索

最新评论

阅读排行榜

评论排行榜