I figured out how to get curl to POST and GET to a webpage. It was working fine and writing the stream to a file perfectly. Now I am trying to convert it to a class called DownloadFile. The end result being able to call member functions like:
download.HTTPPOST(http, postData, filename);
I have the following code in the HTTPPOST member function:
void DownloadFile::HTTPPOST(const char * http, const char *postData, std::string filePath)
{
CURL *curl;
CURLcode res;
std::ofstream fout;
fout.open(filePath, std::ofstream::out | std::ofstream::app);
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if (curl)
{
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, http);
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);
/* send all data to this function */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &fout);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
DownloadFile::setStatus(res);
}
This is the code I have for the write_callback member function:
size_t DownloadFile::write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
std::ofstream *fout = (std::ofstream *)userdata;
for (size_t x = 0; x < nmemb; x++)
{
*fout << ptr[x];
}
return size * nmemb;
}
When I try to build this I get an error:
error C3867: 'DownloadFile::write_callback': non-standard syntax; use '&' to create a pointer to member
Passing the write_callback function by address was working fine before? I did what it suggested '&' operator before the function and recived this error:
error C2276: '&': illegal operation on bound member function expression
I am at a loss trying to figure this out. Why doesn't it recognize the write_callback as an memory address? I am now under the impression that it doesn't have a memory address at compile time so it's confused or something? Any help would be appreciated.
Thanks.