I followed a tutorial to fetch a webpage. It worked but they manually set the URL. I tried changing it to use a URL from a var, but that did not work. I get an error in the terminal "Couldn't resolve host name" I tried main(char*) which gave the same error. I can't seem to find anything online for this. How can I make it so a user-defined var can be used as the URL?
code below.
#include <iostream>
#include <curl/curl.h>
#include <string>
using namespace std;
int main() {
string website;
getline(cin, website);
CURL* curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "init failed\n");
return EXIT_FAILURE;
}
// set up
curl_easy_setopt(curl, CURLOPT_URL, "$website");
// perform
CURLcode result = curl_easy_perform(curl);
if (result != CURLE_OK) {
fprintf(stderr, "download prob: %s\n", curl_easy_strerror(result));
}
curl_easy_cleanup(curl);
return EXIT_SUCCESS;
}
CodePudding user response:
"$website"
is just a string, a piece of text. The variable should be referenced as website
, and since the function is expecting a pointer to an array of characters, you use the c_str()
or data()
method of the class.
curl_easy_setopt(curl, CURLOPT_URL, website.data());