这是修改后的示例,附带libcurl。我通过设置CURLOPT_WRITEDATA将bodyfile传递给函数write_data。它确实将数据写入文件,但指针不等于传递给setopt的指针(curl_handle,CURLOPT_WRITEDATA,指针)。如果我尝试将指针传递给除FILE之外的对象,我会得到SIGSEGV。为什么?我怎样才能传递相同的指针?
代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
FILE *bodyfile;
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
assert(bodyfile == (FILE*) stream); //this assertion fails, but when i comment it, code works. Why?
int written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}
int main(void)
{
CURL *curl_handle;
static const char *headerfilename = "head.out";
FILE *headerfile;
static const char *bodyfilename = "body.out";
curl_global_init(CURL_GLOBAL_ALL);
/* init the curl session */
curl_handle = curl_easy_init();
/* set URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://google.com");
/* no progress meter please */
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
/* open the files */
headerfile = fopen(headerfilename,"w");
if (headerfile == NULL) {
curl_easy_cleanup(curl_handle);
return -1;
}
bodyfile = fopen(bodyfilename,"w");
if (bodyfile == NULL) {
curl_easy_cleanup(curl_handle);
return -1;
}
/* we want the headers to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_WRITEHEADER, headerfile);
/* we want the body to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
/* get it! */
curl_easy_perform(curl_handle);
/* close the header file */
fclose(headerfile);
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
return 0;
}
答案 0 :(得分:2)
如果你删除了一个插入的断言:
printf("body: %p stream: %p\n", bodyfile, stream);
进入write_data,然后添加:
printf("head: %p body: %p\n", headerfile, bodyfile);
打开标题和正文文件后,一切都会变得清晰。
使用bodyfile中的headerfile多次调用write_data,然后使用bodyfile调用一次。我猜在标题响应中的每一行都会调用一次write_data。
curl_easy_setopt的手册页说明了以下关于CURLOPT_HEADERFUNCTION:
如果未设置此选项,或者如果设置了此选项 设置为NULL,但CURLOPT_HEADERDATA (CURLOPT_WRITEHEADER)设置为 除了NULL之外的所有功能 将使用接受响应数据 代替。也就是说,它将是 指定的函数 CURLOPT_WRITEFUNCTION,如果不是 指定或NULL - 默认值, 流写功能。