cURL / Mailing Lists / curl-library / Single Mail

curl-library

Callback function gives crazy "size_t nmemb" value for simple http get operation (C++)

From: Kjetil Volden <kvolden_at_gmail.com>
Date: Sun, 05 Apr 2009 09:29:17 +0200

Hi, I've been googling for the last two hours without finding the answer
to this problem.. I'm using libcurl in a C++ application, and at first I
tried without the callback function, which provided the html to the
standard output. That worked fine, but when I try to utilize the
callback function, as far as I can tell pretty much exactly like the
documentation and other confirmed working examples, something goes off
the hinges. After a little investigation, I could see that the variable
"size_t nmemb" gets a value of "3213310828", and "size_t size" a value
of "751"..

The documentation claims that the total size of the received file is
size*nmemb, which in my case is obviously wrong, as the real file size
is 272413 bytes. I highly suspect "nmemb" to be the offending part here,
but I have no idea what I'm doing wrong.. Any help would be highly
appreciated!

Here's the relevant code:

void Myclass::getpage(){

    CURL *curl;
    CURLcode result;
    std::string url;
    std::string html = "";
   
   
    url = "http://www.amazon.co.uk";
    curl = curl_easy_init();

    if(curl){
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Book::write_html);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &html);
        result = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }
}

size_t Book::write_html(char *buffer, size_t size, size_t nmemb,
std::string *html){
    int result = 0;
   
    if(buffer != NULL){
        html->append(buffer, size*nmemb);
        result = size*nmemb;
    }
    return result;
}
Received on 2009-04-05