新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 函數(shù)的復(fù)用性表現(xiàn)為庫

函數(shù)的復(fù)用性表現(xiàn)為庫

作者: 時(shí)間:2016-11-30 來源:網(wǎng)絡(luò) 收藏
//引入函數(shù)頭文件
#include
#include
//用#pragma comment預(yù)編譯命令,表示這個(gè)程序?qū)⑹褂胕bcurld_imp.lib這個(gè)庫文件
#pragma comment(lib,"libcurld_imp.lib")
//復(fù)制將數(shù)據(jù)寫入文件的回調(diào)函數(shù)

size_t write_data(void *ptr, size_t size,size_t nmemb,FILE *stream) {
int written=fwrite(ptr,size,nmemb,stream);
return written;
}
//使用函數(shù)庫提供函數(shù)實(shí)現(xiàn)的文件下載函數(shù)

CURLcode download( char* url,
char* out)
{
CURL *curl = NULL;
FILE *fp = NULL;
CURLcode res;
//調(diào)用函數(shù)庫中的curl_easy _init()函數(shù)完成初始化
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);
//下載完成,進(jìn)行最后的清理工作
fclose(fp);
return res;
}
else
{
return CURLE_FAILED_INIT;
}
}
//輔助函數(shù),用于獲取文件名
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;
}


關(guān)鍵詞: 函數(shù)復(fù)用性

評(píng)論


技術(shù)專區(qū)

關(guān)閉