Tag Archives: libcurl

Downloading a Web page in C using libcurl

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

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

int main(void)
{
	CURL *curl = curl_easy_init();
	FILE *fptr = fopen("index.html", "w+");
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout);
	curl_easy_setopt(curl, CURLOPT_URL, "http://www.martinbroadhurst.com/");
	curl_easy_perform(curl);
	fclose(fptr);
	curl_easy_cleanup(curl);

	return 0;
}

Reference: libcurl – the multiprotocol file transfer library