Having not got curlpp to work in C ++, I decided to start using libcurl with C instead (for now). Being completely new to C and C ++, this is a bit confusing. I'm not even sure if I can support the C and C ++ functions separately, but as far as I know, this is pure C.
With the help of a friend, I managed to write the output (the contents of the page that curled up) to a text file, but instead I want to put it in a string variable, so I can use the output in other parts of the code. I could just reopen the text file and read its contents, but this is stupid, I want to stop writing to the file and immediately save the string variable.
Recording function
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t written; written = fwrite(ptr, size, nmemb, stream); return written; }
Whole code
#include <iostream> #include "curl/curl.h" #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; /* the function to invoke as the data recieved */ size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { size_t written; written = fwrite(ptr, size, nmemb, stream); return written; } int main(int argc, char *argv[]) { CURL *curl; FILE *fp; CURLcode res; curl = curl_easy_init(); char outfilename[FILENAME_MAX] = "C:\\Users\\admin\\Downloads\\bbb.txt"; if(curl) { char *response = NULL; fp = fopen(outfilename,"wb"); curl_easy_setopt(curl, CURLOPT_URL, "http://www.*hidden*.org/wp-test/lalala.txt"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); curl_easy_cleanup(curl); fclose(fp); } return 0; }
I also hoped that someone would be able to talk in detail about how this function is used. I am using (from php and vb.net) a function similar to this:
function1(ThisIsAvariable,ThisIsAvariableToo) { if ThisIsAvariable = something { Print "gogopowerrrangers" *append* ThisIsAvariableToo } };
which will then be used as:
function1(PassThisAsVariable1,PassThisAsVariable2);
But in the above code, the function
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
Just called like that
write_data
As you can see here:
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
So what is all this for?
(void *ptr, size_t size, size_t nmemb, FILE *stream)
Does curl automatically "populate" certain C / C ++ with other functions than most other languages?