c语言实现IP地址输入的检查

来源:互联网 发布:北京海量数据怎么样 编辑:程序博客网 时间:2024/05/16 06:55
#include <windows.h>#include <stdlib.h>#include <stdio.h>char IPcheck(char * startbuff){char * pbuffer = NULL;char * ptemp = NULL;char ret = 0;char i = 0, j = 0;char temp[8] = {0};ptemp = startbuff;for (i = 0; i < 4; i++){pbuffer = strstr(ptemp,".");/* find "." fail */if( (pbuffer == NULL)&&(i != 3) ){ret = 1;goto out;}/* end of string*/if (i == 3 && pbuffer == NULL){pbuffer = startbuff + strlen(startbuff);}/* between “.” is vaid */if (pbuffer == ptemp){ret = 1;printf("equ\r\n");goto out;}/* slect the between "." string value */for (j = 0; j < pbuffer - ptemp; j++){if (ptemp < pbuffer){temp[j] = ptemp[j];}else{break;}}/* turn to int value form string */if( (atoi(temp) > 0xff)||(atoi(temp) == 0x00) ){printf("no ok, %d\r\n", atoi(temp));ret = 1;goto out;}else{printf("ok, %d\r\n", atoi(temp));}ptemp = pbuffer + 1;for (j = 0; j < 8; j++){temp[j] = 0;}}out:if (ret == 1){printf("fail i = %d\r\n", i);}return ret;}int main(void){char ret = 0;char ip[] = "2.1.12.4";ret = IPcheck(ip);if (ret != 0){/* error */}system("pause");return 0;}


 
阅读全文
0 0
原创粉丝点击