1
我在PicoC上寫了一個小腳本,爲我的Loxone Miniserver Go獲取公共IP地址。所以我一直都知道我的公有IP。我的計劃是獲得知識產權,將其分成4部分,並將整數設置爲程序大綱。爲什麼strcmp()沒有比較chararray和char?
這裏是腳本
// 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,但我沒能解決這個問題。
任何人都可以幫助我解決它嗎?
是的,是有道理的。謝謝! – 2014-10-05 20:16:45