Common code for C + +--download files using Libcurl

Source: Internet
Author: User
Tags curl

#pragma once#include <stdio.h> #include <stdlib.h> #include <curl/curl.h> #ifdef _debug#pragma    Comment (lib, "Libcurld_imp.lib") #else #pragma comment (lib, "Libcurl_imp.lib") #endifclass Curlinit{public:curlinit ()    {Curl_global_init (Curl_global_all);    } ~curlinit () {curl_global_cleanup (); } protected:static curlinit m_initialize;}; Curlinit Curlinit::m_initialize;class Cmyurl{public:cmyurl () {/* init the CURL session */Curl_handl    E = Curl_easy_init ();    } ~cmyurl () {/* Cleanup curl Stuff */curl_easy_cleanup (curl_handle); } public:int get_file (const char* szurl, const char* szfile) {/* set URL to get */Curl_easy_seto                PT (Curl_handle, Curlopt_url, szurl);                /* No progress meter Please */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 files */file* Bodyfile = fopen (Szfile, "WB");            if (Bodyfile = = NULL) {curl_easy_cleanup (curl_handle);        return-1; }/* We want the body is written to this file handle instead of STDOUT */curl_easy_setopt (Curl_hand                Le, Curlopt_writedata, bodyfile); /* Get it!                        */Curl_easy_perform (curl_handle);        /* Close the Body file */fclose (bodyfile);    return 0; }protected:static size_t write_data (void *ptr, size_t size, size_t nmemb, void *stream) {int written = Fwri        Te (ptr, Size, NMEMB, (FILE *) stream);    return written; }protected:curl *curl_handle;};

Common code for C + +--download files using Libcurl

Related Article

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.