1

I do not know how to download the .exe file using curl liblary. I searched the entire internet and found only this code:

int main(void)
{
    CURL *curl;
    FILE *fp;
    CURLcode res;
    char *url = "http://stackoverflow.com";
    char outfilename[FILENAME_MAX] = "page.html";
    curl = curl_easy_init();                                                                                                                                                                                                                                                           
    if (curl)
    {   
        fp = fopen(outfilename,"wb");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);
    }   
    return 0;
}

The problem is that this code downloads only the page code. But how to download the .exe/txt file from the cloud (like google drive / GitHub)?

Thank you in advance for your answer.

blackuGT
  • 15
  • 1
  • 4
  • 1
    Asking for tutorials is off-topic for StackOverflow. In any case, you can use the same code, you simply need to provide a URL that actually points directly at the file you want to download, not point to some html page. I think you need to read up on how URLs actually work. – Remy Lebeau Mar 05 '19 at 18:47
  • Really? And just edit "xxxx.html" to "xxxx.exe"? I'll try to do it! Thank you for response! – blackuGT Mar 05 '19 at 18:49
  • You are passing `NULL` to `CURLOPT_WRITEFUNCTION`. What ou want is a function that will write to `fp`. Have a look [here](https://stackoverflow.com/questions/6951161/downloading-multiple-files-with-libcurl-in-c). – super Mar 05 '19 at 18:53
  • The code works as I download a picture from any page. The exe (dropbox) files still download HTML code @RemyLebeau – blackuGT Mar 05 '19 at 19:28
  • 1
    @super If you pass to `NULL` as the `CURLOPT_WRITEFUNCTION` then the default action is to write the data to the file descriptor provided by `CURLOPT_WRITEDATA` in this case `fp`. – Martin York Mar 05 '19 at 19:43
  • I have already corrected this code. @MartinYork – blackuGT Mar 05 '19 at 19:44
  • If all you want to do is download files across the internet it might be easier to use the command line version of `curl` or an alternative like `wget` (easier to use but as less options). – Martin York Mar 05 '19 at 19:44
  • I'm writing an "updater" so I need to do it somehow via c ++ @MartinYork – blackuGT Mar 05 '19 at 19:45
  • It's been a while, but I'm sure the curl docs have this exact code in an example. It's up to you to have the right URL -- that's not a curl problem. – Kenny Ostrom Mar 05 '19 at 20:27
  • @blackuGT there is no difference between downloading a picture file vs any other kind of binary files, like EXEs. When downloading an EXE, if you are getting an HTML page instead, then either you are requesting the wrong URL for the EXE, or the server is more likely sending back an HTML page explaining that there was an error accessing the EXE file, or prompting you for login credentials, etc. You need to pay attention to what the HTTP response is telling you, and/or look at the returned HTML to see what it says. – Remy Lebeau Mar 05 '19 at 20:44
  • Thank you, gentlemen, for your help! – blackuGT Mar 05 '19 at 20:52

1 Answers1

3

I do not know how to download the .exe file using curl liblary. I searched the entire internet and found only this code

well you didn't search hard enough. libcurl's official website is https://curl.haxx.se/libcurl/ and as you can see enter image description here

it has an examples section, located at https://curl.haxx.se/libcurl/c/example.html with lots of examples. 1 of the most simple examples being located at https://curl.haxx.se/libcurl/c/simple.html , and specifically a simple "file download" example can be found here: https://curl.haxx.se/libcurl/c/url2file.html

/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/ 
/* <DESC>
 * Download a given URL into a local file named page.out.
 * </DESC>
 */ 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <curl/curl.h>

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
  size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
  return written;
}

int main(int argc, char *argv[])
{
  CURL *curl_handle;
  static const char *pagefilename = "page.out";
  FILE *pagefile;

  if(argc < 2) {
    printf("Usage: %s <URL>\n", argv[0]);
    return 1;
  }

  curl_global_init(CURL_GLOBAL_ALL);

  /* init the curl session */ 
  curl_handle = curl_easy_init();

  /* set URL to get here */ 
  curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]);

  /* Switch on full protocol/debug output while testing */ 
  curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);

  /* disable progress meter, set to 0L to enable and disable debug output */ 
  curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);

  /* send all data to this function  */ 
  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

  /* open the file */ 
  pagefile = fopen(pagefilename, "wb");
  if(pagefile) {

    /* write the page body to this file handle */ 
    curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

    /* get it! */ 
    curl_easy_perform(curl_handle);

    /* close the header file */ 
    fclose(pagefile);
  }

  /* cleanup curl stuff */ 
  curl_easy_cleanup(curl_handle);

  curl_global_cleanup();

  return 0;
}
hanshenrik
  • 19,904
  • 4
  • 43
  • 89