http://learn.akae.cn/media/ch25s01.html
// strtok.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "stdio.h" #include "string.h" /* int main(void) { char str[] = "root:x::0:root:/root:/bin/bash:"; char *token; token = strtok(str, ":"); printf("%s\n", token); while ( (token = strtok(NULL, ":")) != NULL) printf("%s\n", token); return 0; }*/ char *split_data( char *init_data, char *separator,int num) { char str[] = "root:x:0:root:/root:/bin/bash:"; char *token=NULL; char *p=NULL; int j=0; // strcpy(p,init_data); //token = strtok(&init_data,&separator); token = strtok(init_data, separator); // printf("%s\n", token); for(int i=0;i<num;i++) { if( (token = strtok(NULL, separator)) != NULL) //printf("%s\n", token); j++; } //printf("%s\n", token); return token; } void main(void) { char str[] = "root:x:0:root:/root:/bin/bash:"; char str2[] = "aa,bb,66,99,uu"; char *b="10000"; char a[32]="10000"; /* char *token; int num=3,j=0; token = strtok(str2, ","); // printf("%s\n", token); for(int i=1;i<num;i++) { if( (token = strtok(NULL, ",")) != NULL) // printf("%s\n", token); // token = strtok(str2, ","); j++; } printf("%s\n", token); */ // b=split_data( str2,",",0); strcpy(a,split_data( str2,",",3)); printf("%s\n", a); }