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

控制台结果:

argv[0]=MyExe
argv[1]=-u
argv[2]=zyg
argv[3]=-p
argv[4]=123456
argv[5]=192.168.31.188



你可能感兴趣的:(C/C++)