libcurl question

Has anyone used libcurl much?

I'm using it to make a small program that downloads a file from a http site.

Having some trouble at the minute, program keeps crashing half way through (I think it's something to do with writing to the file). I'm using fopen/fclose for file opening closing and, I believe, that passing NULL when setting CURLOPT_WRITEFUNCTION makes use of the default fwrite.

My code's messier than I'd like it to be now, but I'll trim it down to the download part for ease:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <string>  
#include <iostream>  
#include <curl.h>

using namespace std;

static char errorBuffer[CURL_ERROR_SIZE];

int main(int argc, char* argv[])
{
   string url = "http://fr1.dayz.nu/latest/DayZ_Changelog.txt";
   string filename = "test.txt";
   
   CURL *curl;  
   CURLcode result; 
   FILE *my_file;

   curl = curl_easy_init();

   if (curl)
   {
      my_file = fopen(filename.c_str(), "wb");  // Write binary
      
      // Set up curl
      curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
      curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL); // Defaults to fwrite
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, my_file);  // Give file to write

      result = curl_easy_perform(curl);

      curl_easy_cleanup(curl);

      fclose(my_file);

      if (result == CURLE_OK)
      {
         cout << "Success";
         return 0;
      }
      else
      {
         cout << "Failure, error: " << result << endl;
         return -1;
      } 
   } // if
}


I appreciate it's a particular question, but if anyone has any ideas I'd be most grateful. I've tried writing a local write function that calls fwrite (just in case the default option wasn't doing so) and I get the same crash.

The crash is a program has stopped working message box.

Note, in order to chomp down my code somewhat, I've copied/pasted/typed parts of it. So there might be the odd typo. The actual code compiles fine, just crashes at run-time
Last edited on
Looks like you need to define the write function. I added
1
2
3
4
size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
 return fwrite(ptr, size, nmemb, stream);
} 
from http://curl.haxx.se/mail/lib-2011-02/0046.html to your code and it works fine.
Really? That's bizarre, I wrote a very similar local function that didn't work. Perhaps I passed one of the parameters or something, I'll try again tonight when I get home from work.

Did you also set the CURLOPT_WRITEFUNCTION to the my_write_func function too?

Thanks for the help. It's much appreciated.
Last edited on
Did you also set the CURLOPT_WRITEFUNCTION to the my_write_func function too?
Yes, and other then that change and the addition of the function, I only changed the site I downloaded (cnn.com)
Edit: The changelog downloads fine as well.
Last edited on
Awesome, I've clearly messed it up somewhere then.

As I said I'll fix this when I get home and marked it as solved.

Much, much appreciated, thanks a bunch.
Topic archived. No new replies allowed.