cURL / Mailing Lists / curl-library / Single Mail


RE: How to retrieve webpage from curl_easy_perform

From: Patrick Monnerat <>
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

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

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

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

It's normal: please read the documentation:
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

To print a textual error message, use this is much
clearer than "error code 23".

List admin:
Received on 2015-03-03