C libcurl print webpage contents

Go To StackoverFlow.com


I'm a total noob when it comes to C but i found this curl example here http://curl.haxx.se/libcurl/c/getinmemory.html

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

    #include <curl/curl.h>

    struct MemoryStruct {
      char *memory;
      size_t size;

    static size_t
    WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
      size_t realsize = size * nmemb;
      struct MemoryStruct *mem = (struct MemoryStruct *)userp;

      mem->memory = realloc(mem->memory, mem->size + realsize + 1);
      if (mem->memory == NULL) {
        /* out of memory! */ 
        printf("not enough memory (realloc returned NULL)\n");

      memcpy(&(mem->memory[mem->size]), contents, realsize);
      mem->size += realsize;
      mem->memory[mem->size] = 0;

      return realsize;

    int main(void)
      CURL *curl_handle;

      struct MemoryStruct chunk;

      chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */ 
      chunk.size = 0;    /* no data at this point */ 


      /* init the curl session */ 
      curl_handle = curl_easy_init();

      /* specify URL to get */ 
      curl_easy_setopt(curl_handle, CURLOPT_URL, "http://www.example.com/");

      /* send all data to this function  */ 
      curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

      /* we pass our 'chunk' struct to the callback function */ 
      curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);

      /* some servers don't like requests that are made without a user-agent
         field, so we provide one */ 
      curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");

      /* get it! */ 

      /* cleanup curl stuff */ 

       * Now, our chunk.memory points to a memory block that is chunk.size
       * bytes big and contains the remote file.
       * Do something nice with it!
       * You should be aware of the fact that at this point we might have an
       * allocated data block, and nothing has yet deallocated that data. So when
       * you're done with it, you should free() it as a nice application.

      printf("%lu bytes retrieved\n", (long)chunk.size);


      /* we're done with libcurl, so clean it up */ 

      return 0;

It works perfect for me, but is it possible to tweak this thing a bit so it prints the content and not the size of the webpage contents?

Thanks in advance!

2012-04-03 22:35
by Stefan
Too lazy to inspect the code, but I would think that chunk.memory is the contents of the page - Corbin 2012-04-03 22:38
And how can i read from chunk.memory then? Could you provide a link or something that would be coo - Stefan 2012-04-03 22:40
printf("%s", chunk.memory) - keety 2012-04-03 22:42


Have you tried:

printf("%lu bytes retrieved\n", (long)chunk.size);

printf("%s", chunk.memory);

You might be surprised.

2012-04-03 22:42
by abelenky
I'll try it, thanks - Stefan 2012-04-03 22:43
It Worked, thanks! (Sorry for the noob question btw... - Stefan 2012-04-03 22:46
Do you have an idea how i can get the contents of chunk.memory into a char? sprintf doesn't work for m - Stefan 2012-04-03 23:18
Your question makes no sense. A "char" is a single-byte. You can't put an entire web page into a single letter! chuck.memory is ALREADY a char* (pointer to an array of chars). What exactly do you mean by "sprintf doesn't work for me" - abelenky 2012-04-04 02:18