Sunday, 15 January 2012

c++ - Simple way to download a file with cURL -



c++ - Simple way to download a file with cURL -

in program, want download few files. took curl , used code (taken , modified little bit here download file using libcurl in c/c++):

#include "curl.h" using namespace std; size_t write_data(void *ptr, size_t size, size_t nmemb, file *stream) { size_t written; written = fwrite(ptr, size, nmemb, stream); homecoming written; } int dlzip(){ curl *curl; file *fp; curlcode res; string url = "http://wordpress.org/extend/plugins/about/readme.txt"; char outfilename[filename_max] = "/users/me/desktop/bbb.txt"; curl = curl_easy_init(); if (curl) { fp = fopen(outfilename,"wb"); curl_easy_setopt(curl, curlopt_url, url.c_str()); 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); } homecoming 0; }

but nil happened , there weren't file on desktop :-/

what problem code? or if have simple function use, give me ?

thanks!

check below code

#include <cstdio> #include <curl/curl.h> #include <curl/types.h> #include <curl/easy.h> #include <string> using namespace std; size_t write_data(void *ptr, size_t size, size_t nmemb, file *stream) { size_t written; written = fwrite(ptr, size, nmemb, stream); homecoming written; } int main(){ curl *curl; file *fp; curlcode res; string url = "http://www.joes-hardware.com/tools.html"; char outfilename[filename_max] = "./mytext.txt"; curl = curl_easy_init(); if (curl) { fp = fopen(outfilename,"wb"); curl_easy_setopt(curl, curlopt_url, url.c_str()); 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); } homecoming 0; }

it works fine. problem - code not able utilize https , hence when url provided when opened in browser , through above code - produce separate responses.

c++ osx curl download

No comments:

Post a Comment