关于获取特定字符?

来源:百度知道 编辑:UC知道 时间:2024/06/14 12:46:52
假如我已经知道某个完整的IP地址,那么怎么获取IP的前3段地址?
比如127.0.0.1,怎么获取127.0.0和127.0.0.(注意2者的区别)

最好理解的方式之1:
#include <stdio.h>
#include <string.h>

void main()
{
char *buf = "127.0.0.1";
int a,b,c;
char buf2[15]={0};

char *p = strrchr(buf,'.');
if(p)
{
memcpy(buf2,buf,p-buf);//如果要127.0.0.再p-buf+1
}

printf("%s\n",buf2);
}