在 C++ 中使用 libcurl 下载多个文件 [英] Downloading multiple files with libcurl in C++

查看:62
本文介绍了在 C++ 中使用 libcurl 下载多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently trying to make an updater for my software project. I need it to be able to download multiple files, I don't mind if they download in sync or one after each other, whatever is easier (file size is not an issue). I followed the example from the libcurl webpage and a few other resources and came up with this:

#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;
}

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

The first issue is if i remove the new file assignments (*url = "http://...") and just try to loop the download code twice, the program simply stops responding. This occurs in any combination of the download being called more than once in the program. The other issue is that I am unable to change the value of the character array outfilename[FILENAME_MAX]. I feel like this is just some silly error I am making but no solution comes to mind. Thank you!

推荐答案

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

  1. Why not put this in a function and call it twice?

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

Your syntax for the arrays is all wrong, plus all the variables inside the loop are local, which means they are destroyed after each loop iteration.

显眼编译器说的.这就是导致您的程序冻结的原因;它陷入无限循环,因为 i 永远不是 >2.

What Conspicuous Compiler said. That's what's causing your program to freeze; it's stuck in an infinite loop because i is never > 2.

将您的代码放入这样的函数中:

Put your code into a function like so:

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);
    }
}

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

And call it twice with the relevant file names and urls:

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

尽管示例功能非常糟糕,但这只是一个示例.您应该更改它以返回错误代码/抛出异常等.

The example function is very bad though, it's just an example. You should alter it to return error codes/throw exceptions, and stuff like that.

这篇关于在 C++ 中使用 libcurl 下载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆