Saving a file using libcurl in C

I am expanding from perl to C, and I'm trying to use the curl library to just save the file from a remote URL, but it's hard for me to find a good example to work with.

Also, I'm not sure if I should use curl_easy_recv or curl_easy_perform

+5
source share
1 answer

I find this resource very developer friendly.

I compiled the source code below:

gcc demo.c -o demo -I/usr/local/include -L/usr/local/lib -lcurl

Basically, it will download the file and save it to your hard drive.

File demo.c

#include <curl/curl.h>
#include <stdio.h>

void get_page(const char* url, const char* file_name)
{
  CURL* easyhandle = curl_easy_init();

  curl_easy_setopt( easyhandle, CURLOPT_URL, url ) ;

  FILE* file = fopen( file_name, "w");

  curl_easy_setopt( easyhandle, CURLOPT_WRITEDATA, file) ;

  curl_easy_perform( easyhandle );

  curl_easy_cleanup( easyhandle );

  fclose(file);

}

int main()
{
  get_page( "http://blog.stackoverflow.com/wp-content/themes/zimpleza/style.css", "style.css" ) ;

  return 0;
}

Also, I find your question is similar to this:

Download file using libcurl in C / C ++

+6

All Articles