#include#include int main() { std::string url = "https://example.com/path/to/file.txt"; CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // Get file name char *fileName = NULL; curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &fileName); std::cout << "File name: " << fileName << std::endl; curl_easy_cleanup(curl); return 0; }
#includeIn this example, we download a file from a remote server and save it to disk using the `write_data` function. The CURL GetFileName function is used to obtain the file name from the URL, which is then used to create a file on disk. The package library for CURL is `libcurl`. It is a free and easy-to-use client-side URL transfer library used to transfer data over the internet using HTTP, HTTPS, FTP, etc.#include 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() { std::string url = "https://example.com/path/to/file.txt"; CURL *curl = curl_easy_init(); curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // Get file name char *fileName = NULL; curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &fileName); // Download file FILE *fp = fopen(fileName, "wb"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); CURLcode result = curl_easy_perform(curl); fclose(fp); if(result != CURLE_OK) { std::cerr << "Error downloading file: " << curl_easy_strerror(result) << std::endl; } else { std::cout << "File downloaded successfully." << std::endl; } curl_easy_cleanup(curl); return 0; }