c++ - libcurl explodes at write_data() function -
c++ - libcurl explodes at write_data() function -
i trying write contents of curl query file if meets validation requirements. code compiles without error upon execution receive next error ->
cryptsy # ./foundgetprice2.o|wgetpaste foundgetprice2.o: rapidjson/include/rapidjson/document.h:911: rapidjson::genericvalue<encoding, allocator>::memberiterator rapidjson::genericvalue<encoding, allocator>::findmember(const rapidjson::genericvalue<encoding, sourceallocator>&) [with sourceallocator = rapidjson::memorypoolallocator<>; encoding = rapidjson::utf8<>; allocator = rapidjson::memorypoolallocator<>; rapidjson::genericvalue<encoding, allocator>::memberiterator = rapidjson::genericmemberiterator<false, rapidjson::utf8<>, rapidjson::memorypoolallocator<> >]: assertion `isobject()' failed. paste can seen here: https://bpaste.net/show/768ea25c86d5
on line 42 of code, may note 2 write lines curl commented out. if write_data() there, works, no validation me no good.
#include "rapidjson/include/rapidjson/document.h" #include "rapidjson/include/rapidjson/rapidjson.h" #include "rapidjson/include/rapidjson/writer.h" #include <cstdio> #include <iostream> #include <fstream> #include <stdio.h> #include <curl/curl.h> #include <unistd.h> using namespace rapidjson; struct mydata { std::fstream *file; std::string *str; }; size_t write_data(void *ptr, size_t size, size_t nmemb, mydata *data) { size_t numbytes = size * nmemb; if (data->file) data->file->write((char*)ptr, numbytes); if (data->str) *data->str += std::string((char*)ptr, numbytes); homecoming numbytes; } std::string tradedata; int getdata() { int count = 0; //begin non terminating loop while(true) { count++; curl *curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, curlopt_url, "http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=155"); std::fstream file("myfile.txt", std::ios_base::out | std::ios_base::ate); std::string json; mydata data; data.file = &file; data.str = &json; //curl_easy_setopt(curl, curlopt_writedata, &data); //curl_easy_setopt(curl, curlopt_writefunction, &write_data); curlcode res = curl_easy_perform(curl); if (res != curle_ok) { std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; } else { file << std::endl; document document; document.parse(json.c_str()); std::string tradeupdate = document["return"]["markets"]["drk"]["lasttradeprice"].getstring(); if (tradeupdate > tradedata){ curl_easy_setopt(curl, curlopt_writedata, &data); curl_easy_setopt(curl, curlopt_writefunction, &write_data); std::cout << std::endl; } else{ std::cout << "there error! " << tradedata << std::endl; } } /* cleanup */ curl_easy_cleanup(curl); } usleep(10000000); } homecoming 0; } //le main int main(void) { getdata(); }
gdb output:
bpaste of gdb bt output
how correctly write info libcurl request next validation?
the programme crashes because of assertion in rapidjson
on line:
std::string tradeupdate = document["return"]["markets"]["drk"]["lasttradeprice"].getstring();
actually lines 42 , 43 commented never assign std::string json
, remains empty, document
empty well.
c++ curl get libcurl
Comments
Post a Comment