1
std::string window::btcunspent()

std::string address = "1BitcoinEaterAddressDontSendf59kuE";

std::string url;
url = "https://blockchain.info/unspent?active=" + address;

const char * c = url.c_str();

CURL *curl;
  CURLcode res;
  std::string readBuffer;

  curl = curl_easy_init();
  if(curl) {

    curl_easy_setopt(curl, CURLOPT_URL, c);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    }

std::ofstream myfile;
myfile.open("btcunspentraw.txt",fstream::out); 
myfile << readBuffer << std::endl;

std::string line;
getline (myfile,line);
boost::replace_all(line, "{\"unspent_outputs\"", "");
boost::replace_all(line, ":[", "");

myfile.close();

return readBuffer;

I am trying to save the response of this api call to a file for use later , however i can't seem to get the coding right. For some reason it refuses to create the file and even if i manually create the file, it is not writing anything.

The output is as follows

{

"unspent_outputs":[

    {
        "tx_hash":"f15c0393d76d26632f9416040a8205da1ff2e294e3ab793425fc95f51a249d36",
        "tx_hash_big_endian":"369d241af595fc253479abe394e2f21fda05820a0416942f63266dd793035cf1",
        "tx_index":827313,
        "tx_output_n": 0,
        "script":"76a914759d6677091e973b9e9d99f19c68fbf43e3f05f988ac",
        "value": 1000000,
        "value_hex": "0f4240",
        "confirmations":235503
    },

    {
        "tx_hash":"9f93847f8a773afb6873dea9f4104647a490d3af70f19f519ff57901f8216b45",
        "tx_hash_big_endian":"456b21f80179f59f519ff170afd390a4474610f4a9de7368fb3a778a7f84939f",
        "tx_index":827483,
        "tx_output_n": 1,
        "script":"76a914759d6677091e973b9e9d99f19c68fbf43e3f05f988ac",
        "value": 100000,
        "value_hex": "0186a0",
        "confirmations":235500
    },

    {
        "tx_hash":"47963eed62de8862d6d0ea1aebf2e18720677b428e5f9fa5e027ce81956c9f51",
        "tx_hash_big_endian":"519f6c9581ce27e0a59f5f8e427b672087e1f2eb1aead0d66288de62ed3e9647",
        "tx_index":836518,
        "tx_output_n": 0,
        "script":"76a914759d6677091e973b9e9d99f19c68fbf43e3f05f988ac",
        "value": 1000000,
        "value_hex": "0f4240",
        "confirmations":235264
    },

I want the final result to just be a file containing the values of these lines as follows :-

tx_hash_big_endian, value

I am also unsure as to how to remove the rest of the unwanted data. Is there a boost function i can use?

Minato
  • 91
  • 1
  • 2
  • 7
  • Maybe you should start checking for error conditions and states. – Captain Obvlious Jul 30 '15 at 20:26
  • possible duplicate of [Download file using libcurl in C/C++](http://stackoverflow.com/questions/1636333/download-file-using-libcurl-in-c-c) – m.s. Jul 30 '15 at 20:27
  • Please show the actual code you are using to create the file and write to it. – Remy Lebeau Jul 30 '15 at 22:04
  • edited to add code, expected output and what kind of result i want to end up with. Thanks , hadn't noticed my paste had excluded the problem area. – Minato Jul 30 '15 at 23:08

0 Answers0