将curl输出保存到文件

时间:2015-07-30 20:23:16

标签: c++ curl

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;

我正在尝试将此api调用的响应保存到稍后使用的文件,但是我似乎无法正确编码。由于某种原因,它拒绝创建文件,即使我手动创建文件,它也没有写任何东西。

输出如下

{

"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
    },

我希望最终结果只是一个包含这些行值的文件,如下所示: -

tx_hash_big_endian,值

我也不确定如何删除其余的不需要的数据。我可以使用增强功能吗?

0 个答案:

没有答案