In failing to get curlpp
for C++ working, I have decided to start using libcurl
with C instead (for now). Being completely new to both C and C++, this is getting a little confusing. I'm not even sure if I can keep C and C++ functions apart, but as far as I know, this is pure C.
With the help of a friend, I have managed to write the output (page contents that curl picked up) to a textfile, but I want to put it in a string variable instead, so I can use the output in other parts of the code. I could just re-open the textfile and read its contents but that's silly, I want to stop writing to a file and just save to a string variable immediately.
The write function
/* the function to invoke as the data received */
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;
}
The entire 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 was also hoping someone could elaborate on how this function is used, exactly. I am used (from php and vb.net) to a function like this:
function1(ThisIsAvariable,ThisIsAvariableToo)
{
if ThisIsAvariable = something
{
Print "gogopowerrrangers" *append* ThisIsAvariableToo
}
};
Which would then be used like:
function1(PassThisAsVariable1,PassThisAsVariable2);
But in the code above, the function
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
Is simply called like this
write_data
As you can see here:
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
So what's all this for?
(void *ptr, size_t size, size_t nmemb, FILE *stream)
Does curl automatically "fill" those in or does C/C++ work differently with functions than most other languages?
See Question&Answers more detail:
os