I'm trying to make a program that scans a txt file line by line, then takes the data from that line and downloads a url formed from that data. what i mean is if the line it read was "banana" it would do www.example.com/banana. i can't seem to figure out curl, could anyone please help me?
#include <curl/curl.h>
#include <iostream>
#include <string>
usingnamespace std;
string data;
size_t download(char* buf, size_t size, size_t nmemb, void* userP)
{
//size*nmemb is the size of the buf(fer)
for (int c = 0; c<size*nmemb; c++)
{
data.push_back(buf[c]);
}
return size*nmemb; //return the number of bytes we handled
}
int main()
{
curl_global_init(CURL_GLOBAL_ALL); //pretty simple
CURL* conHandle = curl_easy_init(); //make an "easy" handle
curl_easy_setopt(conHandle, CURLOPT_URL, "http://www.example.com");
curl_easy_setopt(conHandle, CURLOPT_VERBOSE, 1L); //outputs status
curl_easy_setopt(conHandle, CURLOPT_WRITEFUNCTION, &download); //set our callback to handle data
curl_easy_perform(conHandle); //get the file
cout << data << endl; //should output an html file (if the set url is valid)
system("pause");
return 0;
}
This is a dead simple example lacking error checking (with the exception of the verbose setting) and all that good stuff. For more information, check out these links: