gpt4 book ai didi

c - 每当打印 CURL_WRITEFUNCTION 中的 ptr 时都会输出奇怪的字符。

转载 作者:行者123 更新时间:2023-12-02 05:25:20 24 4
gpt4 key购买 nike

我遇到了一些问题,这是我的代码(我使用的是 C):

#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <json/json.h>

size_t callback_func(void *ptr, size_t size, size_t count, void *stream) {
//json_object *json_obj = json_tokener_parse(ptr);
printf ("%s",(char*)ptr);

return count;

}

int main(void)
{
CURL *curl;
CURLcode res;

curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://stream.twitter.com/1/statuses/filter.json?track=http");
curl_easy_setopt(curl, CURLOPT_USERPWD, "Firrus:password");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback_func);
curl_easy_perform(curl);



/* always cleanup */
curl_easy_cleanup(curl);


}

return 0;
}

问题是,每次打印 ptr 时,顶部也会输出三个奇怪的(看似随机的)字符,例如77D 或 6DA。这些字符是什么意思?我怎样才能删除它们?

最佳答案

根据文档,回调函数的工作方式如下:

Function pointer that should match the following prototype: size_t function( void *ptr, size_t size, size_t nmemb, void *userdata); This function gets called by libcurl as soon as there is data received that needs to be saved. The size of the data pointed to by ptr is size multiplied with nmemb, it will not be zero terminated. Return the number of bytes actually taken care of. If that amount differs from the amount passed to your function, it'll signal an error to the library. This will abort the transfer and return CURLE_WRITE_ERROR. From 7.18.0, the function can return CURL_WRITEFUNC_PAUSE which then will cause writing to this connection to become paused. See curl_easy_pause(3) for further details.

This function may be called with zero bytes data if the transferred file is empty.

.....

Set the userdata argument with the CURLOPT_WRITEDATA option.

The callback function will be passed as much data as possible in all invokes, but you cannot possibly make any assumptions. It may be one byte, it may be thousands. The maximum amount of data that can be passed to the write callback is defined in the curl.h header file: CURL_MAX_WRITE_SIZE.

所以你的回调可能会被调用很多次。您需要将数据存储到您自己的结构中,该结构将跟踪迄今为止读取的数据。

尝试这个解决方案:

#include <stdio.h>
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
size_t size;
char *payload;
}srvresponse;

size_t callback_func(void *ptr, size_t size, size_t count, void *stream) {
//printf("%s", (char*) ptr);

size_t realsize = size * count;
printf("Chuncksize:%lu\n",realsize);
srvresponse *ret = (srvresponse*)stream;
//Increase the payload size
ret->payload = realloc(ret->payload,ret->size + realsize + 1);
//Copy the new data
memcpy(&(ret->payload[ret->size]),ptr,realsize);
//Update the size
ret->size += realsize;
//Terminate the string
ret->payload[ret->size] = 0;
printf("Read so far:%s",ret->payload);
return realsize;

}

int main(void) {
CURL *curl;

srvresponse retdata;
retdata.payload = malloc(1);//We increase the capacity later
retdata.size = 0;

curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://stream.twitter.com/1/statuses/filter.json?track=http");
curl_easy_setopt(curl, CURLOPT_USERPWD, "user:pass");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback_func);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&retdata);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_global_cleanup();


if (retdata.payload){
puts(retdata.payload);
free(retdata.payload);
}
}

return 0;
}

关于c - 每当打印 CURL_WRITEFUNCTION 中的 ptr 时都会输出奇怪的字符。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4763310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com