cURL / Mailing Lists / curl-library / Single Mail

curl-library

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

From: Jason T. Slack-Moehrle <slackmoehrle_at_gmail.com>
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_global_init(CURL_GLOBAL_ALL);

    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);

curl_easy_perform(curl);

 std::ofstream outFile;

outFile.open(lFileName.c_str());

 if (outFile.good())

{

 outFile << stream.str();

}

  outFile.close();

     curl_easy_cleanup(curl);

    curl_global_cleanup();

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++)

{

http_response.push_back(buf[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
string

Any pointers would be appreciated.

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-03-04