Skip to main content

[curl] Download URL to file

cURL provides two kinds of interface to access the internet: easy and multi. Easy interface is synchronous and efficient. The multi interface has asynchronous method also multi-file data transfer ability.

In this tutorial, we are going to demonstrate how to simply download an HTTP file from internet with cURL easy method in C++.

Easy interface

Let’s talk about the easy interface, steps for data transfer purpose can be separated into three.

curl_easy_init()

Initialization an easy-session and get a handle. The handle will be used as an input for the following functions.

curl_easy_setopt()

Setting all the options you want for the internet data transfer including URL, port or the way to write data.

curl_easy_perform()

After the options you want for the upcoming transfer has been settled. you can call the curl_easy_perform() function. It will do the entire job and won’t return until the job has been done or failed.

curl_easy_cleanup()

After the job has been done, the curl_easy_leanup() method will clean up the easy-session handle for you.

Here is a short demonstration of file downloading from http://www.example.com. We are going to save it to example.html in your local folder.

#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
    CURL *curl;
    FILE *fp;
    CURLcode res;
    char *url = "http://www.example.com";
    char outfilename[FILENAME] = "example.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;
}

Options of curl_easy_setopt() are listed below:

Network Options Function
CURLOPT_URL URL to work on
CURLOPT_WRITEFUNCTION Callback for writing data
CURLOPT_WRITEDATA Data pointer to pass to the write callback

Successful run will give you the ability to download the html file you want like this :-D

enter image description here

keywords: cURL, C++, Code::Block

Comments

Popular posts from this blog

[MinGW] Fix [Linker error] undefined reference to '_imp__curl_easy_init'

Recently, I found some error during the compiling progress of my curl program with Code::Blocks. The error keep showing in Code::Blocks like this: [Linker error] undefined reference to `_imp__curl_easy_init' I try to repair the Linker settings and Search directories but found out that there’s nothing wrong with it. It turns out the MinGW version has been modified to Win64 version last time when I was trying to deal with some W64 programs few days ago. I found that the MinGW download directly from the official website cannot be used in Code::Block properly. Hence, I use tdm-gcc instead . Here’s the procedure to fix the curl reference error. Download the tdm32 bundle from: http://tdm-gcc.tdragon.net/ Install MinGW/TDM(32-bit) by just clicking Next in the next few steps After the installation process, the MinGW will be installed under C:\TDM-GCC-32 Open your Code::Blocks program and go to Settings>Compiler Change the setting as followed Now th...

[curl]First curl program with Code::Block

Code::Block is tiny powerful cross-platform IDE for C/C++ program. However, the pre-installed complier is 32-bit. Both curl and openSSL is needed in this tutorial. Hence, it is really important to make sure if they are also installed accordingly. Compiler of your Code::Block can be check from: Setting>Toolchan executables It shows that the compiler is located at C:\MinGW and it is Win32 based. Both Curl and openSSL need to be download for this tutorial.It is highly recommended to install all the program with win32 version!! 1. Download and install the openSSL openSSL can be download from this location. https://slproweb.com/products/Win32OpenSSL.html In this demo, full version of v1.1 is installed. The openSSL folder will be located at C:\OpenSSL-Win32 2. Download and install curl You can find the guideline to setup curl properly from previous article. Just need to remind again that Win32 version is needed in this case. https://aconcaguac...

[pi] Headless pi connection with VNC on Mac system

Sometimes we have to access our pi with limited resources like lacking of monitor or keyboard. The Headless mode is useful to connect to your pi with another computer or your laptop without additional screen! Now let’s get started to give it a try! Setting Raspberry pi board Connection to pi connect to raspberry pi board with ssh pi@rapberrypi.local and input your password(Default password has been setted to raspberry ) Configue the VNC Input sudo raspi-config Navigate to Advance Option Select [P3 VNC] and click [OK] After setting up of the VNC, reboot the pi sudo reboot On Mac Downlaod VNC viewer from: https://www.realvnc.com/en/download/viewer/ After installed the VNC viewer, Input the IP address of raspberry pi and click Continue . Now we have successfully connect to our pi with VNC! Improve the resolution We have justed connected to the pi with VNC. However, the screen is quite small and the resolution wasn’t really good. How can ...