C语言分割字符串strtok函数
int argc = 0; char** argv = (char **) malloc(30 * sizeof(char *)); char *token = NULL; const char* delim = ","; const char* str = "MyExe,-u,zyg,-p,123456,192.168.31.188"; char *params = malloc(strlen(str)+1); memcpy(params, str, strlen(str)+1); token = strtok(params, delim); int index = 0; while(token != NULL) { int length = strlen(token)+1; argv[index] = (char *)malloc(length); memcpy(argv[index],token,length); index++; token = strtok(NULL, delim); } argc = index; int i=0; for(;i<index;i++){ printf("argv[%d]=%s\n",i,argv[i]); } printf("\n");
输出结果
argv[0]=MyExe argv[1]=-u argv[2]=zyg argv[3]=-p argv[4]=123456 argv[5]=192.168.31.188