我試圖使用捲曲庫與此代碼我的服務器下載測試文件:段錯誤使用捲曲
#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string.h>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
int main(void)
{
CURL *curl;
FILE *fp;
CURLcode res;
char *url = "http://pixhost.tk/test.txt";
char outfilename[FILENAME_MAX] = "/Users/Nathan/Desktop";
curl = curl_easy_init();
if (curl)
{
fp = fopen(outfilename, "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 0;
}
並且編譯,就像這樣:
$ gcc main.c -lcurl -o curltest
但是,當我執行它我得到一個Segmentation Fault錯誤。我該怎麼做才能糾正這個問題?
我把我的賭注放在了2. – a3nm 2011-05-01 02:14:31
我也是a3_nm。看起來他正試圖打開一個目錄進行寫入。 – 2011-05-01 03:07:01