开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
ubuntu
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
libcurl
问题(Question):
小弟我是C的新手,最近想要用curl来下载远端的档案
但想要判断使用者输入的是网页还是档案
如果是网页则程式结束
否则就下载档案
其他资讯如下,
喂入的资料(Input):
(1) www.google.com.tw
(2) https://curl.haxx.se/download/curl-7.47.1.tar.bz2
程式码(Code):(请善用置底文网页, 记得排版)
源自:http://stackoverflow.com/questions/1636333/download-file-using-libcurl-in-c-c
[B
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
FILE *fp;
CURLcode res;
//char *url = "www.google.com.tw"; //(1)
char *url = "https://curl.haxx.se/download/curl-7.47.1.tar.bz2";//(2)
///////////////////////////////////////
/*假设是(1) 则不下载,若是(2),则下载*/
///////////////////////////////////////
char outfilename[100] = "output.tar.bz2";
curl = curl_easy_init();
if (curl)
{
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}