C语言中const char *字符进行切割实现

将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;
}
相关推荐
wwj888wwj30 分钟前
Ansible基础(复习1)
linux·运维·ansible
yj_xqj42 分钟前
Linux network启动报错 && nmcli 的使用
linux·运维·服务器
程序猿编码1 小时前
eBPF代理:让SSH进程“溯源”,找到背后的客户端IP
linux·tcp/ip·ssh·ebpf
Shepherd06191 小时前
【IT 实战】解决 TP-Link USB 无线网卡在 Linux/PVE 下识别为存储设备的问题
linux·运维·服务器
开开心心_Every1 小时前
免费轻量电子书阅读器,多系统记笔记听书
linux·运维·服务器·神经网络·安全·机器学习·pdf
存储服务专家StorageExpert1 小时前
DELL EMC isilon/PowerScale 存储的健康检查方法
linux·运维·服务器·netapp存储·emc存储
熊文豪1 小时前
当系统在后台偷偷“记账“:KES 性能观测体系深度解析
linux·运维·服务器·数据库
哦豁灬2 小时前
ThinkPad X220 安装 Arch Linux 完美指南
linux·服务器·thinkpad·arch linux
自动化智库2 小时前
库卡机器人定义全局变量
linux·运维·机器人
Yiyi_Coding2 小时前
BUG列表:如何定位线上 OOM ?
java·linux·bug