1

Loxone Miniserver Go のパブリック IP アドレスを取得するために、PicoC で小さなスクリプトを作成しています。したがって、私は常に自分のパブリック 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);
}

ドキュメントも既に読みましたが、この問題を修正できませんでした。

誰かがそれを修正するのを手伝ってくれますか?

4

1 に答える 1

1

PicoC についてはわかりませんが、ここでの問題は C で発生する問題と同じだと思います。

strcmp は文字列を比較します。文字列と char を比較しても意味がありません。文字列の長さが 1 文字の場合は、 chars を直接比較する必要があります。または、文字列の長さが 1 文字ではありません。この場合、文字と等しくなりません。

特定のケースでは、文字列ではなく文字のみを比較する必要があります。

if (ip[i] != '.' || ip[i] != '\0')
于 2014-10-04T19:50:43.257 に答える