php - Using libcurl in c++, writing into file -
i started using curl. far have managed program response , save file/string. using response apis , process them. i'd using market interpretation test various trading systems. purpose plan save responses tempfile, process file , save a-ready-to-use data one. however, no matter string or file, output one-line type.
i wondering if there way make curl directly write in file, 'sorted' in lines. mean if there response like:
4stmay2017 - gold - open price: 1150, close price:1200 3stmay2017 - gold - open price: 1060, close price:1170 2stmay2017 - gold - open price: 1100, close price:1050 1stmay2017 - gold - open price: 1000, close price:1100
that's way i'd file like, can sort new data (because market dynamic, , requests i'd make every minute or so), , not on 1 line. way have write write separate algorithm slow me down.
what code looks like:
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <curl/curl.h> #include <string> using namespace std; size_t size = 0; size_t write_to_string(void *ptr, size_t size, size_t count, void *stream) { ((string*)stream)->append((char*)ptr, 0, size*count); return size*count; } int main() { curl *curl; curlcode res; curl_global_init(curl_global_all); curl = curl_easy_init(); if (curl){ string out = string("someurl"); curl_easy_setopt(curl, curlopt_url, out.c_str()); string response; curl_easy_setopt(curl, curlopt_ssl_verifypeer, false); curl_easy_setopt(curl, curlopt_writefunction, write_to_string); curl_easy_setopt(curl, curlopt_writedata, &response); res = curl_easy_perform(curl); if (res != curle_ok) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; }
Comments
Post a Comment