Thread: curl problem

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    90

    curl problem

    iam reading the web page using curl socket.
    but some of my data lost.
    code:
    Code:
    #include <stdio.h>
    #include <curl/curl.h>
    #include <curl/curl.h>
    #include <curl/easy.h>
    #include <curl/types.h>
    
    size_t write_data_balance(void *buffer, size_t size, size_t nmemb, void *userp);
    int main(void)
    {
      CURL *curl;
      CURLcode res;
    
    
      curl = curl_easy_init();
      if(curl) {
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data_balance);
        curl_easy_setopt(curl, CURLOPT_URL, "http://yahoo.com");
    	 res = curl_easy_perform(curl); 
    	// curl_easy_cleanup(curl);
    
      }
      return 0;
    }
    
    size_t write_data_balance(void *buffer, size_t size, size_t nmemb, void *userp)
    {
           char *response;
            response = (char*)buffer;
    	    
    printf("*********** &#37;s \n",response);
    
    }
    some of my data lost
    how can i get full data.
    thank u,sree
    Last edited by cnu_sree; 10-26-2007 at 06:28 AM. Reason: changing printf statement

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you're printing userp, not buffer.
    And you're also ignoring the length of the data, and instead assuming that it ends in \0.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    90
    sorry
    i forget to change i tried to print buffer only.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Reading the manual page is a wonderful thing, you should try it sometime.
    Code:
    #include <stdio.h>
    #include <curl/curl.h>
    #include <curl/curl.h>
    #include <curl/easy.h>
    #include <curl/types.h>
    
    size_t write_data_balance(void *buffer, size_t size, size_t nmemb, void *userp);
    int main(void)
    {
      CURL *curl;
      CURLcode res;
    
      curl = curl_easy_init();
      if(curl) {
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1 ); /* DEBUG - for those who RTFM */
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data_balance);
        curl_easy_setopt(curl, CURLOPT_URL, "http://yahoo.com");
        res = curl_easy_perform(curl); 
        curl_easy_cleanup(curl);
      }
      return 0;
    }
    
    size_t write_data_balance(void *buffer, size_t size, size_t nmemb, void *userp)
    {
        char *response;
        response = (char*)buffer;
        /* NOT \0 terminated, so use the passed size information */
        printf("*********** %.*s\n",size*nmemb,response);
        return size * nmemb; /* V.Important */
    }
    
    
    
    
    
    $ gcc new.c -lcurl
    $ ./a.exe
    * About to connect() to yahoo.com port 80 (#0)
    *   Trying 66.94.234.13... * connected
    * Connected to yahoo.com (66.94.234.13) port 80 (#0)
    > GET / HTTP/1.1
    Host: yahoo.com
    Accept: */*
    
    < HTTP/1.1 301 Moved Permanently
    < Date: Fri, 26 Oct 2007 15:03:39 GMT
    < Location: http://www.yahoo.com/
    < Connection: close
    < Transfer-Encoding: chunked
    < Content-Type: text/html; charset=utf-8
    <
    *********** The document has moved <A HREF="http://www.yahoo.com/">here</A>.<P>
    <!-- p1.rc.scd.yahoo.com uncompressed/chunked Fri Oct 26 08:03:39 PDT 2007 -->
    
    * Closing connection #0
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    90
    thank u it's working fine.
    but one small problem
    i want all the data in one variable.
    so how it will be possible.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you use the userp variable to point to some kind of thing which you can use to accumulate the whole string, through repeated calls to your callback function.

    It's in the manual.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM