在 C++ 中使用 libcurl 下载多个文件

llk*_*llk 3 c++ file download libcurl

我目前正在尝试为我的软件项目制作更新程序。我需要它能够下载多个文件,我不介意它们是同步下载还是一个接一个下载,无论哪个更容易(文件大小不是问题)。我遵循了 libcurl 网页和其他一些资源中的示例,并提出了这个:

#include <iostream>
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}
int main(void){
    for (int i = 0; i < 2;){        //download 2 files (loop twice)
        CURL *curl;
        FILE *fp;
        CURLcode res;
        char *url = "http://sec7.org/1024kb.txt";  //first file URL
        char outfilename[FILENAME_MAX] = "C:\\users\\grant\\desktop\\1024kb.txt";
        curl = curl_easy_init();
        if (curl){
            fp = fopen(outfilename,"wb");
            curl_easy_setopt(curl, CURLOPT_URL, url);
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
            curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
            res = curl_easy_perform(curl);
            curl_easy_cleanup(curl);
            fclose(fp);
        }
         url = "http://sec7.org/index.html"; //I want to get a new file this time
         outfilename[FILENAME_MAX] = "C:\\users\\grant\\desktop\\index.html";

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

第一个问题是,如果我删除新文件分配 ( *url = "http://...") 并尝试将下载代码循环两次,程序就会停止响应。这发生在程序中多次调用下载的任意组合中。另一个问题是我无法更改字符数组的值outfilename[FILENAME_MAX]。我觉得这只是我犯的一些愚蠢的错误,但没有想到解决方案。谢谢!

Set*_*gie 6

  1. 为什么不把它放在一个函数中并调用它两次?

  2. 你的数组语法都是错误的,而且循环中的所有变量都是局部的,这意味着它们在每次循环迭代后都会被销毁。

  3. 什么显眼的编译器说。这就是导致您的程序冻结的原因;它陷入无限循环,因为iis never > 2

将您的代码放入如下函数中:

void downloadFile(const char* url, const char* fname) {
    CURL *curl;
    FILE *fp;
    CURLcode res;
    curl = curl_easy_init();
    if (curl){
        fp = fopen(fname, "wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用相关文件名和 url 调用它两次:

downloadFile("http://sec7.org/1024kb.txt", "C:\\users\\grant\\desktop\\1024kb.txt");
downloadFile("http://sec7.org/index.html", "C:\\users\\grant\\desktop\\index.html");
Run Code Online (Sandbox Code Playgroud)

尽管示例功能非常糟糕,但这只是一个示例。你应该改变它以返回错误代码/抛出异常,以及类似的东西。

  • 哈哈谢谢你。有时我会很着迷于修复一些小错误,以至于忘记了让它变得简单并将事情放在一个函数中。我感谢赛斯的回应,我将详细说明带有一些错误处理的代码。 (2认同)