C + + libcurl-based file download

Source: Internet
Author: User

First, the environment-based configuration is not described in detail here, it is important to ensure that the required library files are loaded in

To download the file via Libcurl, the method is implemented as follows:

#include <stdio.h> #include <curl/curl.h> #include "DownloadInfo.h"/*********************************** * Create By:mengxiaoxin DATE:2014/12/9 *//********* * Libcurl Write callback function */size_t Write_ Data (void *ptr, size_t size, size_t nmemb, FILE *stream) {size_t written = Fwrite (ptr, size, nmemb, stream); return written ;} /* Function:libcurl Connection initialization download file Parameters: (const char* URL, const char outfilename[ Filename_max]) URL: URL of the file to download outfilename: Download file specified filename */int download_file (const char* URL, const char OUTFI Lename[filename_max]) {CURL *curl; FILE *FP; Curlcode res;/* calls Curl_global_init () to initialize Libcurl */res = Curl_global_init (Curl_global_all), if (CURLE_OK! = res) {printf (" Init Libcurl failed. "); Curl_global_cleanup (); return-1;} /* Call the Curl_easy_init () function to get the easy interface type pointer */curl = Curl_easY_init (); if (curl) {fopen_s (&fp,outfilename, "WB");/* Call curl_easy_setopt () set transfer options */res = curl_easy_setopt (Curl,                  Curlopt_url, URL);  if (res! = CURLE_OK) {fclose (FP); Curl_easy_cleanup (curl);    return-1;}  /* Implement callback functions to complete user-specific tasks */res = curl_easy_setopt (Curl, Curlopt_writefunction, Write_data) based on the transport options set by Curl_easy_setopt ()  if (res! = CURLE_OK) {fclose (FP); Curl_easy_cleanup (curl); return-1;} /* Implement callback functions to complete user-specific tasks */res = curl_easy_setopt (Curl, Curlopt_writedata, FP) according to the transport options set by Curl_easy_setopt (), if (res! = Curle_o  K) {fclose (FP); Curl_easy_cleanup (curl); return-1;}                               res = curl_easy_perform (curl); Call the Curl_easy_perform () function to complete the transfer task fclose (FP);/* Check for errors */if (res! = CURLE_OK) {fprintf (stderr, "Curl_easy_  Perform () failed:%s\n ", Curl_easy_strerror (res)); Curl_easy_cleanup (curl); return-1;}                                     /* Always cleanup */curl_easy_cleanup (curl); Call Curl_easy_cleanup () to free memory}curl_global_cleanup (); return 0;}

The above code is based on official changes

The official example procedure is as follows:

/*************************************************************************** *                                  _   _ ____  _ * Project ___| | | | _ \| | *                             / __| | | | |_) | | *                            |  (__| |_| | _ <| |___ * \___|\___/|_| \_\_____| * Copyright (C) 1998-2012, Daniel Stenberg, <[email protected]>, et al. * * This software is licensed as De scribed in the file COPYING, which * you should has received as part of this distribution. The terms * is also available at http://curl.haxx.se/docs/copyright.html. * * May opt-to-use, copy, modify, merge, publish, distribute and/or sell * copies of the software, and permit persons To whom the software are * furnished to does so, under the terms of the COPYING file. * * This software are distributed on a "as is" basis, without WARRANTY of any * KIND, either express or implied. * *******************************************************/#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, siz  E, 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_noprogr   ESS, 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, pag     Efile); /* Get it!     */Curl_easy_perform (curl_handle);  /* Close the header file */fclose (pagefile);   }/* Cleanup curl Stuff */curl_easy_cleanup (curl_handle); return 0;}



C + + libcurl-based file download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.