How to download an http file?

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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <curl/curl.h>
#include <iostream>
#include <string>
using namespace 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:

http://curl.haxx.se/libcurl/c/libcurl-tutorial.html
http://curl.haxx.se/libcurl/c/libcurl-easy.html
http://curl.haxx.se/libcurl/c/libcurl-errors.html
Thanks man
Topic archived. No new replies allowed.