使用libcurl下载目录中的所有文件

Thi*_*Thi 3 c++ ftps download libcurl

我是libcurl的新手,并找到了从ftp服务器下载单个文件的方法.现在我的要求是下载目录中的所有文件,我猜它不受libcurl的支持.请在libcurl上建议如何下载目录中的所有文件,还是有类似libcurl的其他库?

提前致谢.

men*_*tat 8

这是一段代码示例.

static size_t GetFilesList_response(void *ptr, size_t size, size_t nmemb, void *data)
{
    FILE *writehere = (FILE *)data;
    return fwrite(ptr, size, nmemb, writehere);
}

bool FTPWithcURL::GetFilesList(char* tempFile)
{
    CURL *curl;
    CURLcode res;
    FILE *ftpfile;

    /* local file name to store the file as */
    ftpfile = fopen(tempFile, "wb"); /* b is binary, needed on win32 */ 

    curl = curl_easy_init();
    if(curl) 
    {
        curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.example.com");
        curl_easy_setopt(curl, CURLOPT_USERPWD, "username:password");
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile);
        // added to @Tombart suggestion
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, GetFilesList_response);
        curl_easy_setopt(curl, CURLOPT_DIRLISTONLY, 1);

        res = curl_easy_perform(curl);

        curl_easy_cleanup(curl);
    }

    fclose(ftpfile); //


    if(CURLE_OK != res) 
        return false;

    return true;
}
Run Code Online (Sandbox Code Playgroud)

  • 是不是缺少写功能?`curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,GetFilesList_response);` (5认同)