cURL / Mailing Lists / curl-library / Single Mail

curl-library

RE: Binary data

From: Sigal Algranaty <sigala_at_itemfield.com>
Date: Sun, 6 Apr 2003 11:36:31 +0200

Hi,
Thanks for your answer.
I don't know what's the problem, but I can't make it work.
All I wanna do is download binary file.
That's my program. Seem quite simple, but what ha[[ens is really weird.
Either the file that I'm opening is left empty, or it is filled with
data that is not an image.
Can anyone please tell me what I'm doing wrong?

Thanks,
Sigal

size_t write_callback(void *buffer,
                      size_t size,
                      size_t nitems,
                      void *userp)
{
  FILE *file = (FILE *)userp;
  size_t write;
  size *= nitems;
  write = fwrite(buffer, size, nitems, file);
  return size;
}
int main(void)
{
  CURL *curl;
  CURLcode res;
  FILE *data;
  data=fopen("sigal", "wb");
  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL,
"http://skychasers.net/SUN.jpg");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
    curl_easy_setopt(curl, CURLOPT_FILE, data); /* write here */
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
        fclose(data);
   }
  return 0;
}

-----Original Message-----
From: curl-library-admin_at_lists.sourceforge.net
[mailto:curl-library-admin_at_lists.sourceforge.net] On Behalf Of Daniel
Stenberg
Sent: Thursday, March 27, 2003 4:32 PM
To: libcurl Mailing list
Subject: Re: Binary data

On Thu, 27 Mar 2003, Sigal Algranaty wrote:

> Sorry if this question is for something that appears in documentation.
>
> How can I get binary data from url?

libcurl downloads everything binary by default.

> If I have a url which is an image, for example, and I want it to be
> downloaded to an image file on my disc, how do I do it?

Something like this (untested code) might work:

int main(void)
{
  CURL *curl;
  CURLcode res;
  FILE *storehere;

  storehere=fopen("dumpit", "wb"); /* b for windows people */

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL,
"http://imagesite.com/image.png");

    curl_easy_setopt(curl, CURLOPT_WRITEDATA, storehere); /* write here
*/

    /* If you use windows and libcurl as a DLL, you must also set a
callback
       function with CURLOPT_WRITEFUNCTION */

    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}

-- 
 Daniel Stenberg -- curl, cURL, Curl, CURL. Groks URLs.
-------------------------------------------------------
This SF.net email is sponsored by:
The Definitive IT and Networking Event. Be There!
NetWorld+Interop Las Vegas 2003 -- Register today!
http://ads.sourceforge.net/cgi-bin/redirect.pl?keyn0001en
-------------------------------------------------------
This SF.net email is sponsored by: ValueWeb: 
Dedicated Hosting for just $79/mo with 500 GB of bandwidth! 
No other company gives more support or power for your dedicated server
http://click.atdmt.com/AFF/go/sdnxxaff00300020aff/direct/01/
Received on 2003-04-06