我正在PicoC写一个小脚本来获取我的Loxone Miniserver Go的公共IP地址。所以我一直都知道我的公共IP。我的计划是获取IP,将其分成4个部分并将整数设置为程序集。
这是脚本
// write program here in PicoC
char* append(char* addThis, char* toThis)
{
char* destination = (char*)malloc( strlen( addThis ) + strlen( toThis ) + 1 );
strcpy( destination, toThis );
strcat( destination, addThis );
return destination;
}
while(TRUE)
{
char *result = NULL;
result = httpget("checkip.dyndns.org","");
int j = 0;
char* p = NULL;
p = strstrskip(result,"Current IP Address: ");
j=strfind(p, "<", FALSE);
char ip[strlen(p)-j];
strncpy(ip,p,j);
char *first = malloc(4);
char *second = malloc(4);
char *third = malloc(4);
char *fourth = malloc(4);
char *tmp = NULL;
for (int i = 0; ip[i] != '\0'; i++) { //Made by me, so it may not be the most efficienet way
tmp = malloc(4);
if (strcmp(ip[i], ".") || ip[i] != '\0') //Error
tmp = append(tmp, &ip[i]);
if (strcmp(ip[i], ".") && first == NULL) { //Error
setlogtext("testing");
setlogtext(tmp);
strcpy(frist, tmp);
setlogtext(first);
} else if (strcmp(ip[i], ".") && second == NULL) { //Error
strcpy(second, tmp);
} else if (strcmp(ip[i], ".") && third == NULL) { //Error
strcpy(third, tmp);
} else if (strcmp(ip[i], ".") && fourth == NULL) { //Error
strcpy(fourth, tmp);
}
if (strcmp(ip[i], ".") || ip[i] == '\0')
free(tmp);
}
free(tmp);
setlogtext(first);
setoutput(0, atoi(first));
setoutput(1, atoi(second));
setoutput(2, atoi(third));
setoutput(3, atoi(fourth));
sleeps(15);
}
我也已阅读documentation,但我无法解决此问题。
有人可以帮我解决吗?
答案 0 :(得分:1)
我不知道PicoC,但我想这里的问题与你在C中的问题相同。
strcmp比较字符串,它就是它的方式。比较字符串和字符是没有意义的:要么你的字符串是1个字符长度,在这种情况下你应该直接比较字符;或者你的字符串不是1个字符长度,在这种情况下它不会等于字符。
在您的具体情况下,您应该只比较字符,而不是字符串:
if (ip[i] != '.' || ip[i] != '\0')