Downloading multiple files with libcurl in C++(在 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!
推荐答案
为什么不把它放在一个函数中并调用它两次?
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 下载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 C++ 中使用 libcurl 下载多个文件
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01