cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Problem with CURLOPT_WRITEDATA getting Binary data

From: Kalidoss Sivasamy <kalidoss2_at_gmail.com>
Date: Sun, 30 May 2010 10:36:44 -0500

Ok, I modified the code, first not null terminate ( //mem->memory[mem->size]
= 0;). Also, changed the print routine to print each char at a time.

I still see the same result. There is something else missing here, not sure
what. I thought downloading binary data into memory is straight forward, but
it doesn't seem to be. As I mentioned before, I can save to a file with
CURLOPT_FILE without any issues.

#include <stdlib.h>
#include <stdio.h>
#include <string>

#include <curl/curl.h>

struct MemoryStruct {
      char *memory;
      size_t size;
};

void printText(char* s){
    int len = strlen(s);
    for (int i = 0; i < len; i++) {
        printf("%c", *s++);
    }
}

void *myrealloc(void *ptr, size_t size)
{
  /* There might be a realloc() out there that doesn't like reallocing
     NULL pointers, so we take care of it here */
  if(ptr)
    return realloc(ptr, size);
  else
    return malloc(size);
}

size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)data;

  mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
  if (mem->memory) {
    memcpy(&(mem->memory[mem->size]), ptr, realsize);
    mem->size += realsize;
    //mem->memory[mem->size] = 0;
  }

  return realsize;

}

size_t write_callback(void *buffer,
                      size_t size,
                      size_t nitems,
                      void *userp)
{
  FILE *file = (FILE *)userp;
  size_t write;
  //size *= nitems;
  printf("DATA [%s]\n", buffer);

  write = fwrite(buffer, size, nitems, file);
  return write;

}

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

  struct MemoryStruct chunk;

  chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
  chunk.size = 0; /* no data at this point */

  curl_global_init(CURL_GLOBAL_ALL);
  curl = curl_easy_init();

  if(curl) {
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");

    curl_easy_setopt(curl, CURLOPT_URL,"
http://www.buildeazy.com/plans/test.pdf");

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);

    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    printText(chunk.memory);

    if (chunk.memory)
          free(chunk.memory);

   }

  return 0;
}

On Sun, May 30, 2010 at 9:56 AM, Kamil Dudka <kdudka_at_redhat.com> wrote:

> On Sunday 30 of May 2010 15:36:37 Kalidoss Sivasamy wrote:
> > Yes, I took it from the examples and it works for fetching web pages.
> >
> > Here I am trying to get a pdf test file but it prints only the first
> > few bytes for some reason. You should be able to compile and run it to
> > see what I am referring to.
>
> So the problem is probably in the printing, not in getting the content,
> as Tor already suggested. You want to use fwrite(), instead of printf(),
> to stream out the binary data.
>
> Kamil
>

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-05-30