Skip to content

Instantly share code, notes, and snippets.

@johnnylord
Last active December 21, 2020 21:51
Show Gist options
  • Save johnnylord/3c95664a625728d4874b551f5c7fe996 to your computer and use it in GitHub Desktop.
Save johnnylord/3c95664a625728d4874b551f5c7fe996 to your computer and use it in GitHub Desktop.
Using libcurl to download remote file in C
#include <stdio.h>
#include <curl/curl.h>
int main(int argc, char **argv)
{
CURL *curl;
FILE *fp;
int result;
// Output file
fp = fopen(argv[2], "wb");
// Initialize curl handler
curl = curl_easy_init();
// Set options of curl handlder
curl_easy_setopt(curl, CURLOPT_URL, argv[1]);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
// Download the file from specified url(argv[1])
result = curl_easy_perform(curl);
// Check result
if (result == CURLE_OK)
printf("Download successful!\n");
else
printf("ERROR: %s\n",curl_easy_strerror(result));
fclose(fp);
curl_easy_cleanup(curl);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment