1

In my program, I want to download a few files. So I took cURL and used this code (taken and modified a little bit from 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);
    return 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);
    }
    return 0;
}

But nothing happened and there weren't any file on my desktop :-/

What is the problem with my code? Or if you have a simple function to use, could you give me ?

Thanks!

Community
  • 1
  • 1
Tiwenty
  • 733
  • 1
  • 10
  • 17

1 Answers1

0

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

It works fine. The problem is - this code is not able to use https and hence when the url provided by you when opened in browser and through above code - produce separate responses.

Gaurav K
  • 2,864
  • 9
  • 39
  • 68
  • http://stackoverflow.com/questions/25540547/how-to-receive-a-zip-file-from-server-c-c/25545023#25545023 This is how it will work – Gaurav K Aug 28 '14 at 09:14