cURL / Mailing Lists / curl-library / Single Mail

curl-library

RE: How to retrieve webpage from curl_easy_perform

From: Patrick Monnerat <Patrick.Monnerat_at_datasphere.ch>
Date: Tue, 3 Mar 2015 18:23:12 +0100

 
Josh Angstadt wrote:

> Specifically, I replaced the READFUNCTION and READDATA with
WRITEFUNCTION and WRITEDATA respectively. At first I changed the
callback function to this:

> static size_t read_callback(void *ptr, size_t size, size_t nmemb,
void *stream)
> {
> curl_off_t nread;
> char *r = (char*)ptr;
> size_t retcode = fwrite(r, size, nmemb, (FILE*)stream);
> nread = (curl_off_t)retcode;
> return retcode;
> }

To write to a file, you do not need this function: it is basically a
null wrapper to the fwrite function (that is default, as previously
noted). Just pass an opened FILE * to CURLOPT_WRITEDATA (not the result
variable local to main).

> When I ran that, it would get the curl_easy_perform call, then the
program would freeze and crash.

There can be many cause of this crash: what do you pass to
CURLOPT_WRITEDATA? If it's not a FILE *, then the fwrite call will
fail or even segfault (you receive in 'stream' what you've set with
CURLOPT_WRITEDATA).

> When I tried replacing the contents of the callback function with this
test:

> fprint("%s.*", size, (char*)stream);
> return 0;

> It gave me the error code 23 I mentioned before.

It's normal: please read the documentation:
http://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html
<quote>
Your callback should return the number of bytes actually taken care of.
If that amount differs from the amount passed to your callback function,
it'll signal an error condition to the library. This will cause the
transfer to get aborted and the libcurl function used will return
CURLE_WRITE_ERROR.
</quote>

To print a textual error message, use
http://curl.haxx.se/libcurl/c/curl_easy_strerror.html: this is much
clearer than "error code 23".

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