curl-library
delete incomplete files
Date: Tue, 02 Dec 2014 17:29:43 +0100
Hi there,
I'am using libcurl in a c++ project for downloading files from websites
and storing them on my harddisk.
Sometimes there are errors on the websites, that cause a download to
never finish. I added a timeout to handle that.
Unfortunately curl creates an (empty) file in my filesystem which then
can't be processed by the rest of my projects algorithm.
Is there any way to delete files after aborting the download?
You will find the curl methods I'm using below.
Best regards
Timo
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_to_file(string url, string resultfile)
{
char *download_url = string_to_char(url);
char *store_path = string_to_char(resultfile);
CURL *curl;
FILE *fp;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
fp = fopen(store_path,"wb");
curl_easy_setopt(curl, CURLOPT_URL, download_url);
/* example.com is redirected, so we tell libcurl to follow
redirection */
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows
NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed\n");
curl_easy_cleanup(curl);
fclose(fp);
}
else
{
errmsg("curl was not initialized correctly");
}
}
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2014-12-02