Member Avatar for Levo

Hi, I am using the following code to download files from the internet:

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
    size_t written;
    written = fwrite(ptr, size, nmemb, stream);
    return written;
}

int main(int argc, char** argv)
{
	FILE *downloaded_file;
	if ( (downloaded_file = fopen (download_path , "w" ) ) != NULL )
	{
		CURL *curl;
		CURLcode res;
		curl = curl_easy_init();
		if(curl)
		{
			curl_easy_setopt(curl, CURLOPT_URL, "www.asd.com/files/file_to_download.rar");
			curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
			curl_easy_setopt(curl, CURLOPT_WRITEDATA, downloaded_file);
			res = curl_easy_perform(curl);
			curl_easy_cleanup(curl);
		
			if (res == CURLE_OK)
			{
				printf("Download complete!\n");
			}
		}
		fclose(downloaded_file);
	}
}

How can I measure the current download speed (e.g. every second) and the remaining time to complete the download?

Recommended Answers

All 2 Replies

write_data(...) is callback function which will be called every time each new data portion arrives from internet. So basically you should measure download speed and remaining time in your function write_data():
1. save current time in local variable time1
2. then download speed will be written/(time1-time2) 3. save current time in global variable time2
4. remaining time will be bytes remaining/download speed Current time you can get with functions in time.h.
As about bytes remaining, i am not sure. It should be => total fize size - current file size. The problem is how to get total file size. Check libcurl documentation - maybe there are some functions which probes file sizes on net before downloading it.

good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.