paulogp

Download de ficheiro da Web

Jul 13th, 2011
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. // Apple Xcode
  2. // download file from web
  3. // gcc -lcurl main.c -o teste
  4.  
  5.  
  6. //#define CURL_STATICLIB
  7.  
  8. #include <stdio.h>
  9. #include <curl/curl.h>
  10.  
  11.  
  12. size_t func_write_data(void *the_ptr, size_t the_size, size_t the_nmemb, FILE *the_stream)
  13. {
  14.     size_t the_written = fwrite(the_ptr, the_size, the_nmemb, the_stream);
  15.     return the_written;
  16. }
  17.  
  18.  
  19. int main (int argc, const char * argv[])
  20. {
  21.     // realiza o download de um ficheiro da web para o computador
  22.     CURL *the_curl;
  23.     FILE *the_file;
  24.     CURLcode the_curl_code;
  25.  
  26.     char *the_url = "http://elmicro.com/files/cosmic/clanguage.pdf";
  27.     char the_output_file[FILENAME_MAX] = "/Users/.../Desktop/clanguage.pdf";
  28.  
  29.     the_curl = curl_easy_init();
  30.     if (the_curl)
  31.     {
  32.         the_file = fopen(the_output_file, "wb");
  33.         curl_easy_setopt(the_curl, CURLOPT_URL, the_url);
  34.         curl_easy_setopt(the_curl, CURLOPT_WRITEFUNCTION, func_write_data);
  35.         curl_easy_setopt(the_curl, CURLOPT_WRITEDATA, the_file);
  36.         the_curl_code = curl_easy_perform(the_curl);
  37.         curl_easy_cleanup(the_curl);
  38.         fclose(the_file);
  39.     }
  40.  
  41.     return 0;
  42. }
Add Comment
Please, Sign In to add comment