//引进函数库头文件
#include
#include
//用#pragma comment预编译指令,表明这个程序将运用ibcurld_imp.lib这个库文件
#pragma comment(lib,”libcurld_imp.lib”)
//仿制将数据写入文件的回调函数
size_t write_data(void *ptr, size_t size,size_t nmemb,FILE *stream) {
int written=fwrite(ptr,size,nmemb,stream);
return written;
}
//运用函数库供给函数完结的文件下载函数
CURLcode download( char* url,
char* out)
{
CURL *curl = NULL;
FILE *fp = NULL;
CURLcode res;
//调用函数库中的curl_easy _init()函数完结初始化
curl = curl_easy_init();
if (curl)
{
fp=fopen(out,”wb”);
curl_easy_setopt(curl,CURLOPT_URL, url);
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl,CURLOPT_WRITEDATA, fp);
res=curl_easy_perform(curl);
curl_easy_cleanup(curl);
//下载完结,进行最终的整理作业
fclose(fp);
return res;
}
else
{
return CURLE_FAILED_INIT;
}
}
//辅佐函数,用于获取文件名
void getfilename(char* url, char* name)
{
char* pos = strrchr(url,”/”);
if (NULL!=pos)
{
strcpy(name,pos+1);
}
}
int main(int argc,char* argv[])
{
if (2!=argc)
{
puts(“usage:gethttp://www.51hei.com/f/keil3.rar”);
return -1;
}
char* url = argv[1];
char name[32];
getfilename(url,name);
CURLcode res = download(url,name);
if(CURLE_OK==res)
{
printf(“great! download %s as %s succerfully.”,url,name);
}
else
{
printf(“sorry:cannot download %s.”,url);
}
return 0;
}