3

the cURL example given here

http://curl.haxx.se/libcurl/c/getinmemory.html

shows how to get a url to memory. i would like to change the above code to use in c++ without much change. I would like to replace the malloc and realloc with something else. Is there a way to get the above code to work by using an STL like list or vector of strings to save the url to memory?

Prasanth Madhavan
  • 12,657
  • 15
  • 62
  • 94

2 Answers2

3

as nick has pointed out, luckyspin.org/?p=28 gave me the answer.

static int writer(char *data, size_t size, size_t nmemb,
                  std::string *buffer)
{
  int result = 0;

  if (buffer != NULL){
    buffer->append(data, size * nmemb);
    result = size * nmemb;
  }

  return result;
}
Prasanth Madhavan
  • 12,657
  • 15
  • 62
  • 94
  • you could use the std::string .size() method, so the 'result' isn't really needed. This would allow you to return void rather than int. 'static' means this function can't be used outside of the translation unit, it's not really needed either. – Chris Huang-Leaver Mar 10 '11 at 13:35
1

If you use C++, you can try curlpp : see this question : How do you make a HTTP request with C++?

my2c

Community
  • 1
  • 1
neuro
  • 14,948
  • 3
  • 36
  • 59