tags:

views:

92

answers:

3

This is my code:

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

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
char test[];
size_t  write_data(char *ptr, size_t size, size_t nmemb, FILE *stream)
{
  char buf[size*nmemb+1];
  char * pbuf = &buf[0];
  memset(buf, '\0', size*nmemb+1);
  size_t i = 0;
  for(;  i < nmemb ; i++){
    strncpy(pbuf,ptr,size);
    pbuf += size;
    ptr += size;
  }

  printf("%s",buf);
  test=new test[size*nmemb+1];
  return size * nmemb;
}

int main()
{
    CURL *curl_handle;
    curl_handle = curl_easy_init();

    curl_easy_setopt(curl_handle,   CURLOPT_URL, "http://www.google.com");
    curl_easy_setopt(curl_handle,   CURLOPT_NOPROGRESS  ,1);
    curl_easy_setopt(curl_handle,   CURLOPT_WRITEFUNCTION,&write_data);
    curl_easy_perform(curl_handle);
    curl_easy_cleanup(curl_handle);
    return 0;
}

Why I am getting this error:

../src/get_webpage.cpp:9: error: storage size of ‘test’ isn't known
../src/get_webpage.cpp: In function ‘size_t write_data(char*, size_t, size_t, FILE*)’:
../src/get_webpage.cpp:23: error: expected type-specifier before ‘test’
../src/get_webpage.cpp:23: error: expected ‘;’ before ‘test’
+2  A: 

Change char test[]; to char *test;

nos
it works ....how can i access the test value in main function if i return the test it says error???
lal
#Ial test is global and you can access it like you would do with any local variable. But the catch is the callback has to be hit for test to contain data. This may need more code to be added.
Praveen S
+1  A: 

test=new test[size*nmemb+1];

This is C++ code, not C. Change it to

test = malloc(size*nmemb+1);

Also it is recommended to free it somewhere. Maybe at the end of main or before allocating it.

free(test);

Good luck.

bluntkhan
A: 
char buf[size*nmemb+1];

Is C99 not C89. For greater nmemb -> stackoverflow, better here also use a dyn. allocation

char *buf = calloc(1,size*nmemb+1), *pbuf = buf;
size_t i = 0;
if( !buf )
  { error-handling needed }
...
free(buf);