C split sample
from http://www.cnblogs.com/oomusou/archive/2009/05/09/c_split.html
/*
(C) OOMusou 2009 http://oomusou.cnblogs.com
Filename : split.c
Compiler : Visual C++ 9.0
Description : Demo how to implement split() in C
Release : 05/09/2009 1.0
*/
#include <stdio.h>
#include <string.h>
void split(char **arr, char *str, const char *del) {
char *s = strtok(str, del);
while(s != NULL) {
*arr++ = s;
s = strtok(NULL, del);
}
}
int main() {
char *str = "10,20,30";
char *arr[3];
const char *del = ",";
int i = 0;
split(arr, str, del);
while(i<3)
printf("%s\n", *(arr+i++));
}
/*
(C) OOMusou 2009 http://oomusou.cnblogs.com
Filename : split.c
Compiler : Visual C++ 9.0
Description : Demo how to implement split() in C
Release : 05/09/2009 1.0
*/
#include <stdio.h>
#include <string.h>
void split(char **arr, char *str, const char *del) {
char *s = strtok(str, del);
while(s != NULL) {
*arr++ = s;
s = strtok(NULL, del);
}
}
int main() {
char *str = "10,20,30";
char *arr[3];
const char *del = ",";
int i = 0;
split(arr, str, del);
while(i<3)
printf("%s\n", *(arr+i++));
}
in c++, must transfer string to char*!
int main() {
char *arr[3];
char *s = (char*)"_";
char buff[80];
strcpy (buff, "DR_1_Disable.xml");
split(arr, buff, s);
while(i<3)
printf("%s\n", *(arr+i++));
}
char *arr[3];
char *s = (char*)"_";
char buff[80];
strcpy (buff, "DR_1_Disable.xml");
split(arr, buff, s);
while(i<3)
printf("%s\n", *(arr+i++));
}
留言
張貼留言