0

I have a binary file in a remote web server.

And I want to download it with curl in c++.

But in one condition, I cannot write it to a file.

So I have to hold it into char array.

What I have tried so far is,

size_t WriteHtmlCallback(void *ptr, size_t size, size_t count, void *stream)
{
    ((string*)stream)->append((char*)ptr, 0, size * count);

    return size * count;
}

std::string html;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteHtmlCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &html);

But html string loses binary data.

What's the solution?

Joshua Son
  • 1,839
  • 6
  • 31
  • 51
  • possible duplicate of [libcurl output to variable instead of textfile](http://stackoverflow.com/questions/8020640/libcurl-output-to-variable-instead-of-textfile) – jpw Mar 23 '14 at 02:58
  • Although the answer I flagged as duplicate isn't a perfect match, I believe you'll find the answer there. – jpw Mar 23 '14 at 02:59
  • 1
    @jpw Actually, it was quite good example for me. Writing it into stringstream, but the result looks the same. Anyway, thanks a lot. – Joshua Son Mar 23 '14 at 03:05

0 Answers0