每当打印 CURL_WRITEFUNCTION 中的 ptr 时都会输出奇怪的字符。
我遇到了一些问题,这是我的代码(我使用的是 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。这些字符是什么意思?我怎样才能删除它们?
I'm having a bit of an issue, here is my code (I'm using 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;
}
The problem is that, every time ptr is printed, three weird (seemingly random) characters are also outputted at the top, e.g. 77D or 6DA. What do these characters mean? How can I remove them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据文档,回调函数的工作方式如下:
因此您的回调可能会被调用多次。
您需要将数据存储到您自己的结构中,该结构将跟踪迄今为止读取的数据。
尝试这个解决方案:
In according with the documentation the callback functions works on this way:
So your callback could be called many times.
You need to store the data into your own structure which will keep track about the data read so far.
Try this solution: