cURL / Mailing Lists / curl-library / Single Mail

curl-library

Newb:Save image to a file:C++

From: Erico <chubychoco_at_yahoo.com>
Date: Sat, 28 Feb 2009 08:54:04 -0800 (PST)

Hi,

I'm trying to grab an image from a site and save it to a file. I can get the image file that I wanted but I can't view the result file, I noticed that the result file has a bit different file size from the original file and If I open the images file using text-editor, its only difference is at the beginning of the file (has an extra newline and maybe another data not displayed).

Am I doing it wrong?

//code
#include <iostream>
#include <curl/curl.h>
#include <stdio.h>

using namespace std;

int main(){
    CURL *image;
    CURLcode imgresult;
    FILE *fp;
    char *url = "http://www.google.co.id/intl/en_com/images/logo_plain.png";
   
        image = curl_easy_init();
        if( image ){
            // Open file
            fp = fopen("google.png", "w");
            if( fp == NULL ) cout << "File cannot be opened";
           
            curl_easy_setopt(image, CURLOPT_WRITEFUNCTION, NULL);
            curl_easy_setopt(image, CURLOPT_WRITEDATA, fp);
            curl_easy_setopt(image, CURLOPT_URL, url);
           
            // Grab image
            imgresult = curl_easy_perform(image);
            if( imgresult ){
                cout << "Cannot grab the image!\n";
            }
        }
       
        // Clean up the resources
        curl_easy_cleanup(image);
        // Close the file
        fclose(fp);
    return 0;
}

Thank you
Received on 2009-02-28