字符分割函数
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";