1.获取网络文件大小:
#include <curl/curl.h>
size_t get_size_struct(void* ptr, size_t size, size_t nmemb, void* data) {
return (size_t)(size * nmemb);
}
double GetRemoteFileSize(const char* url) {
CURL* curl;
CURLcode res;
double size = 0.0;
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, get_size_struct);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
res = curl_easy_perform(curl);
res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &size);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_getinfo() failed: %s\n", curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
return size;
}
2.获取本地文件大小
#include <fstream>
double GetLocalFileSize(const char* filePath)
{
FI