cURL / Mailing Lists / curl-library / Single Mail

curl-library

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

From: Lars Nilsson <chamaeleon_at_gmail.com>
Date: Mon, 4 Mar 2013 11:05:08 -0500

On Mon, Mar 4, 2013 at 10:27 AM, Jason T. Slack-Moehrle
<slackmoehrle_at_gmail.com> wrote:
> 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

A complete (compile-able) standalone program would help avoid this
question: Are your write functions by any chance non-static methods in
a class?

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