0

我正在尝试使用 libcurl 获取网页的标题内容并尝试将其存储在字符缓冲区中。程序给出了分段错误。我是 libcurl 的初学者,我不确定问题出在哪里。这是下面的代码。

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

#include <curl/curl.h>



struct buf {
    char *buffer;
    size_t bufferlen;
    size_t writepos;
} buffer = {0};

struct buf HeaderBuffer;

static size_t write_data(void *ptr, size_t size, size_t nmemb, char *string)
{
  size_t nbytes = size*nmemb;
    if (!HeaderBuffer.buffer) {
        HeaderBuffer.buffer = malloc(1024);
        HeaderBuffer.bufferlen = 1024;
        HeaderBuffer.writepos = 0;
    }
    if (HeaderBuffer.writepos + nbytes < HeaderBuffer.bufferlen) {
        HeaderBuffer.bufferlen = 2 * HeaderBuffer.bufferlen;
        HeaderBuffer.buffer = (char *)realloc(HeaderBuffer.buffer, buffer.bufferlen);
    }
    assert(HeaderBuffer.buffer != NULL);
    memcpy(HeaderBuffer.buffer+HeaderBuffer.writepos,ptr,nbytes);
    return nbytes;

}

int main(void)
{
  CURL *curl_handle;

  curl_global_init(CURL_GLOBAL_ALL);

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


  curl_easy_setopt(curl_handle, CURLOPT_URL, "http://example.com");


  /* send all data to this function  */ 
  curl_easy_setopt(curl_handle,CURLOPT_HEADERFUNCTION, write_data); 

  /* get it! */ 
  curl_easy_perform(curl_handle);



printf("%s",HeaderBuffer.buffer);



  /* cleanup curl stuff */ 
  curl_easy_cleanup(curl_handle);

  return 0;
}

编辑 :

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

struct string {
  char *ptr;
  size_t len;
};

void init_string(struct string *s) {
  s->len = 0;
  s->ptr = (char *)malloc(s->len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "malloc() failed\n");
    exit(1);
  }
  s->ptr[0] = '\0';
}

size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
  size_t new_len = s->len + size*nmemb;
  s->ptr =(char *) realloc(s->ptr, new_len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "realloc() failed\n");
    exit(EXIT_FAILURE);
  }
  memcpy(s->ptr+s->len, ptr, size*nmemb);

  //sprintf (s->ptr+s->len,"New header:\n%s\n",(char *)ptr);


  s->ptr[new_len] = '\0';
  s->len = new_len;

  return size*nmemb;
}

int main(void)
{
  CURL *curl;
  CURLcode res;
  char url[50];



  char *ptr=NULL;
  int i=0;

  memset(Buffer,0,255);

  strcpy(url,"http://");
  strncat(url,"74.125.236.176",strlen("74.125.236.176"));

  curl = curl_easy_init();
  if(curl) {
    struct string s;
    init_string(&s);
    curl_easy_setopt(curl, CURLOPT_URL,url);


    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, writefunc);
    curl_easy_setopt(curl, CURLOPT_WRITEHEADER, &s);
    res = curl_easy_perform(curl);



     printf("\n \n %s",Buffer);

    printf("%s\n", s.ptr);




    free(s.ptr);

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

1 回答 1

0

在这里我认为你应该这样做,

HeaderBuffer.bufferlen = nbytes * HeaderBuffer.bufferlen;

由于您不知道 nbytes,它可能大于2*HeaderBuffer.bufferlen.

于 2013-10-14T05:54:15.937 回答