将127.0.0.1以"""."来进行切割,实现如下:
const char * ip = "127.0.0.1";
char *test = new char[100];
strcpy(test, ip);
const char *split = ".";
char *final;
final = strtok(test, split);
while (final)
{
printf("%s\n", final);
final = strtok(NULL, split);
}
return 0;
最终实现效果:
想要将字符转成整形,如下:
#include <stdio.h>
#include <iostream>
//借助strtok实现split
#include <string.h>
int main()
{
const char * ip = "127.0.0.1";
char *test = new char[100];
strcpy(test, ip);
const char *split = ".";
char *final;
final = strtok(test, split);
while (final)
{
printf("%s\n", final);
int c = atoi(final);
printf("data = %d\n", c);
final = strtok(NULL, split);
}
return 0;
}