1.Install libcurl
sudo apt install curl
curl -o cpplibrary.pdf http://www.cesarkallas.net/arquivos/livros/informatica/cpp/The%20C%2B%2B%20Standard%20Library.pdf
2.
#include <chrono> #include <ctime> #include <curl/curl.h> #include <curl/easy.h> #include <iostream> #include <iomanip> #include <stdio.h> std::string get_time_now(bool is_exact = false) { std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now(); time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now); struct tm tm_info = *localtime(&raw_time); std::stringstream ss; ss << std::put_time(&tm_info, "%Y%m%d%H%M%S"); if (is_exact) { std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()); std::chrono::milliseconds mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()); std::chrono::microseconds micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()); std::chrono::nanoseconds nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()); std::uint64_t mills_count = mills.count() - seconds.count() * 1000; std::uint64_t micros_count = micros.count() - mills.count() * 1000; std::uint64_t nanos_count = nanos.count() - micros.count() * 1000; ss << "-" << std::setw(3) << std::setfill('0') << std::to_string(mills_count) << std::setw(3) << std::setfill('0') << std::to_string(micros_count) << std::setw(3) << std::setfill('0') << std::to_string(nanos_count); } return ss.str(); } size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t written = fwrite(ptr, size, nmemb, stream); return written; } void download_file() { std::cout<<get_time_now(true)<<",start in "<<__FUNCTION__<<std::endl; CURL *curl; FILE *fp; CURLcode res; char *url = "https://www.mica.edu.vn/perso/Vu-Hai/EE3490/Ref/The%20C++Standard%20Library%20-%202nd%20Edition.pdf"; char outfilename[FILENAME_MAX] = "/home/fred/C/aaaaa.pdf"; curl = curl_easy_init(); if (curl) { fp = fopen(outfilename, "wb"); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); /* always cleanup */ curl_easy_cleanup(curl); fclose(fp); } std::cout<<get_time_now(true)<<",finish in "<<__FUNCTION__<<std::endl; } int main(void) { download_file(); return 0; }
3.
g++ -std=c++2a -I. *.cpp -o h1 -luuid -lpthread -lcurl
4.
./h1
标签:std,count,via,chrono,libcurl,c++,include,time,now From: https://www.cnblogs.com/Fred1987/p/17430499.html