I am implementing this code but i am receiving an error.
http://curl.haxx.se/libcurl/c/ftpupload.html
The error is in this bit of code.
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
curl_off_t nread;
/* in real-world cases, this would probably get this data differently
as this fread() stuff is exactly what the library already would do
by default internally */
size_t retcode = fread(ptr, size, nmemb, stream);
nread = (curl_off_t)retcode;
fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
" bytes from file\n", nread);
return retcode;
}
Errors are...
IntelliSense: argument of type "void *" is incompatible with parameter of type "FILE *"
and
Error C2664: 'fread' : cannot convert parameter 4 from 'void *' to 'FILE *'
Any tips would be useful. I don't understand why we'd pass a void *stream to the function. What does that even mean? A pointer to a void?
It is called here.
/* we want to use our own read function */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
CURL API
CURLOPT_READFUNCTION
Pass a pointer to a function that matches the following prototype: size_t function( void *ptr, size_t size, size_t nmemb, void *userdata); This function gets called by libcurl as soon as it needs to read data in order to send it to the peer. The data area pointed at by the pointer ptr may be filled with at most size multiplied with nmemb number of bytes. Your function must return the actual number of bytes that you stored in that memory area. Returning 0 will signal end-of-file to the library and cause it to stop the current transfer.
If you stop the current transfer by returning 0 "pre-maturely" (i.e before the server expected it, like when you've said you will upload N bytes and you upload less than N bytes), you may experience that the server "hangs" waiting for the rest of the data that won't come.
The read callback may return CURL_READFUNC_ABORT to stop the current operation immediately, resulting in a CURLE_ABORTED_BY_CALLBACK error code from the transfer (Added in 7.12.1)
From 7.18.0, the function can return CURL_READFUNC_PAUSE which then will cause reading from this connection to become paused. See curl_easy_pause(3) for further details.
Bugs: when doing TFTP uploads, you must return the exact amount of data that the callback wants, or it will be considered the final packet by the server end and the transfer will end there.
If you set this callback pointer to NULL, or don't set it at all, the default internal read function will be used. It is doing an fread() on the FILE * userdata set with CURLOPT_READDATA.
I'm a bit out of my depth.
freadtakes aFILE*as its fourth argument, avoid*does not match. Assuming the argument tostreamwas aFILE*before passing it to the parameter, you need to cast it:fread(..., (FILE*)stream)- 0x499602D2fread()takes the file as the last parameter; not the first. The cast resolution is obviously still correct; just move it =P. - WhozCraig