Download a file from the internet using Libcurl
Hello everyone, I hope you guys are doing well. So I am back with another post where I will be showing you guys how to download a file from the internet with the help of C++ code using the libcurl library. Cool, isn’t it? Well, after you have completely read the post, you guys will also be able to do this cool stuff. So without wasting any time, let’s get started.
Also read: Web Scraping using C++
C++ Code to download file from URL using Libcurl
#include <iostream> #include <curl/curl.h> // Callback function to handle downloaded data size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t written = fwrite(ptr, size, nmemb, stream); return written; } int main() { curl_global_init(CURL_GLOBAL_ALL); CURL *curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/file.txt"); // Replace with the actual URL curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); FILE *file = fopen("downloaded_file.txt", "wb"); // Replace with the desired filename if (!file) { fprintf(stderr, "Failed to open file\n"); return 1; } curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); CURLcode res = curl_easy_perform(curl); // Perform the download if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } fclose(file); curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; }
Explanation of the above program:
The code looks messy, isn’t it. Don’t worry, I’ll be explaining each and every line in detail.
#include <iostream> #include <curl/curl.h>
iostream is the standard input-output library containing definitions for functions like cin and cout, whereas curl/curl.h is for using the libcurl library’s functionalities.
curl_global_init(CURL_GLOBAL_ALL); CURL *curl = curl_easy_init();
This is just the initialization of the libcurl and creating an object out of it, so that later on we can use it’s functionalities for downloading the desired file.
if (curl) { curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/file.txt"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &file); CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } curl_easy_cleanup(curl); }
The above code looks horrible, right? Don’t worry as I will be explaining each line step by step.
line 2 : curl_easy_setopt
function is used to set options for a curl easy handle, which is responsible for a single file transfer. There are 3 arguments inside the function, the first argument curl is the curl object that we created a few lines above, the second argument CURLOPT_URL
, we are instructing libcurl to fetch the file located at the third argument, which is basically the URL of the file that we wish to download.
line 3 : curl_easy_setopt
function and the first argument curl is the same as we discussed above, the second argument CURLOPT_WRITEFUNCTION
tells the function to call the third argument write_data, which in turn is a function
line 4 : curl_easy_setopt
function and the first argument curl is the same as we discussed above, CURLOPT_WRITEDATA
tells libcurl to send all the downloaded data to the location specified by the third argument file.
Simple, isn’t it. The rest of the code is even more simpler.
line 5 : CURLcode
is a datatype defined by libcurl that simply represents a return code from its functions. We make a variable named res of this datatype. curl_easy_perform
initiates and carries out the actual download process.
line 6 and 7: These are just for the purpose of error handling.
line 9: Is just for performing cleanups in the end, which is always a good programming practice.
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t written = fwrite(ptr, size, nmemb, stream); return written; }
This block contains the logic for our write_data
function’s logic. fwrite
is a standard C library function used to write data to a variable. It has 4 arguments :
ptr: A pointer to the data to be written.
size: The size of each element in the data, in bytes.
nmemb: The number of elements to write.
stream: The stream to write to (e.g., a file stream)
at last, we return the number of elements successfully written, which is stored in the variable named written.
FILE *file = fopen("downloaded_file.txt", "wb"); if (!file) { fprintf(stderr, "Failed to open file\n"); return 1; }
This code is just for opening the file that we just downloaded and checking whether we are able to even open it or all our efforts went into vain.
curl_easy_perform(curl);
This line performs the download.
fclose(file); curl_easy_cleanup(curl);
These lines are just for closing the file and performing the cleanup.
With the following code and it’s deep understanding, we are now capable of downloading files from the internet without having the need to use any web browser, thanks to the libcurl library. So, guys with this we have come to an end of our discussion. But don’t worry, I’ll be back with another epic tutorial very soon, so stay tuned till then. Feel free to comment down below. Signing off, goodbye!
Leave a Reply