Libcurl将数据写入数组
Libcurl write data to array
我真的搜索过了。我看过libcurl如何下载并写入数据到文件的例子但我不知道如何写入数组
这是目前为止的代码:
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
int written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}
int main(void)
{
CURL *curl_handle;
FILE *bodyfile;
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://example.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);
fclose(bodyfile);
return 0;
}
您所需要的似乎在libcurl文档中有描述:
CURLOPT_WRITEFUNCTION
应该匹配以下原型的函数指针函数(char *ptr, size_t, size_t nmemb, void *userdata);这函数会在收到数据后立即被libcurl调用需要被拯救。[…]
也就是说你必须实现一个带有
签名的函数size_t my_array_write(char *ptr, size_t size, size_t nmemb, void *userdata);
传递给curl:
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, my_array_write);
然而,我还没有测试它(我不知道一个更简单的方法来实现这一点)。更多信息请参见libcurl文档
相关文章:
- C++:__aligned(__alignof__) 导致字符数组数据出现问题?
- 很好的语法来获取对向量/数组数据的大小引用?
- 数组数据以错误的方式遍历 Python/Matlab
- 访问 Arduino 中的结构数组数据
- 在能够从 Web 浏览器访问大型数组数据的同时存储它的最可行方法是什么?
- 在C 类中声明数组数据
- 如何在 c++ 中将字符串数组数据写入输出文件
- 使用 stl::map 和 stl::unordered_map 对包含大量重复元素的数组数据进行排序
- 检索字符数组数据时出现问题
- 共享库的C 访问数组数据
- 将包含数组数据成员的结构保存到文件
- C++中的LinkedList字符数组数据
- 正在尝试使用if语句检查数组数据
- 在单独的数据中显示数组数据
- 如何在 c++ 中将字节类型的字符数组数据保存到文件中
- 从数组数据中缺少第一个元素的 DLL 返回C++结构
- 如何在C++中表示二进制字节数组数据
- 实现动态数组数据结构时出现"double free or corruption"错误
- 多维数组数据成员的运行时错误
- C++ :使用 * 显示数组数据的条形图