在C ++中使用libcurl下载多个文件 [英] Downloading multiple files with libcurl in C++
问题描述
#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);
返回书面;
}
int main(void){
for(int i = 0; i< 2;){//下载2个文件(循环两次)
CURL * curl;
FILE * fp;
CURLcode res;
char * url =http://sec7.org/1024kb.txt; //第一个文件URL
char outfilename [FILENAME_MAX] =C:\\ersers\\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; //我想要获取一个新的文件
outfilename [FILENAME_MAX] =C:\\ersers\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
}
返回0;
}
第一个问题是如果我删除新的文件分配( * url =http:// ...
),只是尝试循环下载代码两次,程序只是停止响应。这种情况发生在程序中多次下载的任何组合。另一个问题是我无法更改字符数组 outfilename [FILENAME_MAX]
的值。我觉得这只是我做的一些愚蠢的错误,但没有解决方案。谢谢!
-
为什么不把它放在一个函数中并调用两次?
-
您的数组语法错误,循环中的所有变量都是本地的,这意味着它们在每次循环迭代后被销毁。 >
-
什么是明显的编译器说的。这是导致您的程序冻结的原因它被卡在无限循环中,因为
i
永远不会> 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);
}
}
并使用相关文件名和网址调用两次:
downloadFile(http://sec7.org/1024kb.txt,C:\\ersers\\ \\\grant\\desktop\\1024kb.txt);
downloadFile(http://sec7.org/index.html,C:\\\users\\grant\\desktop\\index.html);
示例函数非常糟糕,但这只是一个例子。你应该改变它来返回错误代码/抛出异常,以及类似的东西。
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;
}
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.
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);
}
}
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屋!