I am trying to download a zip file from a url and save it on the appdata directory. I am able to get the appdata directory. Only problem is that the getenv("APPDATA")
function only puts out char*
or std::string
but outfilename[FILENAME_MAX]
only takes char
. Below is my code and I have commented my error exactly so it's easier to see.
I am seeking for a solution to this problem. Thanks for your time!
PS I am open to any suggestions that would reach my end goal of downloading the file to appdata folder. If there is a way that doesn't use curl that's no problem for me:)
#include <stdio.h>
#include <curl/curl.h>
#include <string>
#include <windows.h>
#pragma warning(disable:4996)
using namespace std;
size_t write_data(void* ptr, size_t size, size_t nmemb, FILE* stream)
{
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
int main(void)
{
//Start cURL Download
//following gets the appdata folder
CoInitialize(0);
char* appdata = getenv("APPDATA");
strcat(appdata, "\\zip to save.zip");
printf("Appdata: %s\n", appdata); //this works no problem it prints the appdata location
//start cURL download
CURL* curl;
FILE* fp;
CURLcode res;
char outfilename[FILENAME_MAX] = appdata; //this is the error that fails everything Error C2440 'initializing': cannot convert from 'char *' to 'char [260]'
//Also tried to have appdata as a std::string but it wouldnt take it either
const char* url = "http://urltozip.com/zip.zip";
curl = curl_easy_init();
if (curl)
{
fp = fopen(outfilename, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
//End cURL Download
return 0;
}
EDIT: This fixed my issues. Thanks to @Hasturkun. I just replaced the upper part of the code with this and completely removed char outfilename[FILENAME_MAX] = appdata;
lower in the code.
char outfilename[FILENAME_MAX];
char* appdata = getenv("APPDATA");
strcat(appdata, "\\zip to save.zip");
printf("Appdata: %s\n", appdata);
strcpy(outfilename, appdata);
This fixed my issues. Thanks to @Hasturkun. I just replaced the upper part of the code with this and completely removed char outfilename[FILENAME_MAX] = appdata;
lower in the code.
char outfilename[FILENAME_MAX];
char* appdata = getenv("APPDATA");
strcat(appdata, "\\zip to save.zip");
printf("Appdata: %s\n", appdata);
strcpy(outfilename, appdata);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.