cURL / Mailing Lists / curl-library / Single Mail


Newb:Save image to a file:C++

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


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?

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

using namespace std;

int main(){
    CURL *image;
    CURLcode imgresult;
    FILE *fp;
    char *url = "";
        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
        // Close the file
    return 0;

Thank you
Received on 2009-02-28