码迷,mamicode.com
首页 > 其他好文 > 详细

字符分割函数

时间:2014-10-30 09:28:13      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   sp   div   on   log   


char
*strsep(char **stringp, const char *delim) { char *s; const char *spanp; int c, sc; char *tok; if ((s = *stringp)== NULL) return (NULL); for (tok = s;;)
   { c
= *s++; spanp = delim; do
     {
if ((sc =*spanp++) == c)
       {
if (c == 0) s = NULL; else s[-1] = 0; *stringp = s; return (tok); } } while (sc != 0); } }
测试:
int main (void)
{
    char *pString = "1234;123.112.123.123:8081;GPSDATA;;60;19200;8;N;1;N";
    char *pStrTmp[20]={NULL};
    int i=0;
    pStrTmp[i] = strsep(&pString,";");
    while((pStrTmp[i] != NULL)&&(i<19))
  { i
++; pStrTmp[i] = strsep(&pString,";"); printf("pStrTmp[%d] = %s\n",pStrTmp[i] ); } return 0;
}
运行结果:
pStrTmp[0] = "1234";
pStrTmp[1] = "123.112.123.123:8081";
pStrTmp[2] = "GPSDATA";
pStrTmp[3] = "";
pStrTmp[4] = "60";
pStrTmp[5] = "19200";
pStrTmp[6] = "8";
pStrTmp[7] = "N";
pStrTmp[8] = "1";
pStrTmp[9] = "N";

 

 

 

字符分割函数

标签:style   blog   color   ar   for   sp   div   on   log   

原文地址:http://www.cnblogs.com/chengliangsheng/p/4061682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!