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:
#include <string>
#include <iostream>
#include <curl.h>
usingnamespace std;
staticchar 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
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?
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.