Question

I am transferring a binary file (.exe) with FTP using libcurl, and saving it to a local file. The problem is that after the file is transferred, it is altered and is no longer a valid Win32 application, and doesn't run. Here's how I'm doing it:

CURL *curl;

curl = curl_easy_init();
    FILE* f = fopen("C:\\blah.exe", "w");

if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "ftp://ftp.mysite.com");
    curl_easy_setopt(curl, CURLOPT_USERPWD, "blah:blah");
    curl_easy_setopt(curl, CURLOPT_FTP_FILEMETHOD, CURLFTPMETHOD_SINGLECWD);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &f);
} else {
            fclose(f);
    return CURL_EASY_INIT_FAIL;
}

    fclose(f);

The file is written but is bigger than it is on the FTP server. Like I said, trying to run it results in the "%1 is not a valid Win32 application" error. Did I forget to set an option or something?

Was it helpful?

Solution

You forgot the binary flag. This is the correct code:

 FILE* f = fopen("C:\\blah.exe", "wb");

OTHER TIPS

The reason is that you transfer as ASCII and not as binary. So your end of lines might get broken. Of there are CRs in the binary they might turn into CR LF or the other way around. Tune CURL to make a binary transfer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top