cURL / Mailing Lists / curl-library / Single Mail


Writing cURL response to a file or a string in C++

From: Jason T. Slack-Moehrle <>
Date: Mon, 4 Mar 2013 07:27:39 -0800

Hello All,

I am trying to run cURL to a PHP URL and get back a response and write it
to a file or first a string, then a file, which ever works best.

OSX 10.8, latest cURL.

I have:

CURL* curl;


    curl = curl_easy_init();

 curl_easy_setopt(curl, CURLOPT_URL, jstr.c_str());

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);


 std::ofstream outFile;;

 if (outFile.good())


 outFile << stream.str();





the write_data():

 size_t write_data(char *ptr, size_t size, size_t nmemb, void *userdata)


    std::ostringstream *stream = (std::ostringstream*)userdata;

     size_t count = size * nmemb;

    stream->write(ptr, count);

    return count;


I seem to be crashing at: stream->write(ptr, count);

with simply a bus error

I have also tried:

size_t write_data(char* buf, size_t size, size_t nmemb, void* up)

{ //callback must have this declaration

//buf is a pointer to the data that curl has for us

//size*nmemb is the size of the buffer

 for (int c = 0; c<size*nmemb; c++)




return size*nmemb; //tell curl how many bytes we handled


the url is correct and if I paste the url into a browser it does return the

Any pointers would be appreciated.

List admin:
Received on 2013-03-04