metalx1000

Download a File with C++

Nov 17th, 2016
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. //g++ get.c -o get  `pkg-config --libs --cflags libcurl`
  2. //sudo aptitude install libcurl-dev
  3. #include <stdio.h>
  4. #include <curl/curl.h>
  5. #include <string>
  6.  
  7. size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
  8.     size_t written = fwrite(ptr, size, nmemb, stream);
  9.     return written;
  10. }
  11.  
  12. int main(void) {
  13.     CURL *curl;
  14.     FILE *fp;
  15.     CURLcode res;
  16.     char *url = "https://filmsbykris.com/v7/img/tux.png";
  17.     char outfilename[FILENAME_MAX] = "tux.png";
  18.     curl = curl_easy_init();
  19.     if (curl) {
  20.         fp = fopen(outfilename,"wb");
  21.         curl_easy_setopt(curl, CURLOPT_URL, url);
  22.         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
  23.         curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
  24.         res = curl_easy_perform(curl);
  25.         /* always cleanup */
  26.         curl_easy_cleanup(curl);
  27.         fclose(fp);
  28.     }
  29.     return 0;
  30. }
Add Comment
Please, Sign In to add comment