2

cでwgetを使ってWebページをダウンロードしたいと思います。私はこのコードを書きましたが、試してみると、プログラムは指定された名前の一部のみで名前が付けられたページをダウンロードし、ファイル名に無効なエンコーディングが見つかりました。

ページ名はこんな感じ

test0L���i}�X�����L�������R�td]�{��+`��U{�@ (invalid encoding)

私のプログラムの重要な部分はこれです。

#define PAGE "http://deckbox.org/games/mtg/cards?p="

char *cat_url(char *s1, char *s2)
{
    char *tmp;
    tmp = (char*)malloc(sizeof(char*) * (strlen(s1) + strlen(s2)));
    strcat(tmp, s1);
    strcat(tmp, s2);
    return tmp;
}

void get_card_name(char *pg_name)
{
    int i;
    int fk;
    char *args[6], tmp;

    for (i = 0; i < 8; i++) {
        tmp = itoa(i);
        args[0] = "wget";
        args[1] = "-q";
        args[2] = cat_url(PAGE, &tmp);
        args[3] = "-O";
        args[4] = cat_url("test", &tmp);
        args[5] = NULL;

        if (fork()) {
            wait(&fk);
        } else {
            if (execvp(args[0], args) == -1) {
               error_rep("ERROR.\n");
            }
       }
    }
}

どうすれば問題を解決できますか? ありがとう

4

2 に答える 2

2

問題は次の行にあります。

char tmp;
tmp = itoa(i);

代わりに次のことを試してください。

char tmp[2];   // to store 1 char and '\0'
...
snprintf (tmp, sizeof (tmp), "%d", i);   // portable way to convert int to string
...
args[2] = cat_url(PAGE, tmp); // tmp is a pointer now
...
args[4] = cat_url("test", tmp);

それが役に立てば幸い !

于 2013-04-12T21:13:23.600 に答える