尝试使用libcurl将文件下载到appdata目录



我正在尝试从url下载zip文件并将其保存在appdata目录上。我能够获得appdata目录。唯一的问题是getenv("APPDATA")函数只输出char*std::string,而outfilename[FILENAME_MAX]只输出char。下面是我的代码,我已经准确地注释了我的错误,这样更容易看到。

我正在寻找解决这个问题的方法。谢谢你的时间!p。我愿意接受任何建议,以达到我下载文件到appdata文件夹的最终目标。如果有一种不使用curl的方法,那对我来说没有问题:)

#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: %sn", 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;
} 

编辑:这解决了我的问题。感谢@Hasturkun。我只是用这个替换了代码的上部,并完全删除了代码中较低的char outfilename[FILENAME_MAX] = appdata;

char outfilename[FILENAME_MAX];
char* appdata = getenv("APPDATA");
strcat(appdata, "\zip to save.zip");
printf("Appdata: %sn", appdata); 
strcpy(outfilename, appdata);

这解决了我的问题。感谢@Hasturkun。我只是用这个替换了代码的上部,并完全删除了代码中较低的char outfilename[FILENAME_MAX] = appdata;

char outfilename[FILENAME_MAX];
char* appdata = getenv("APPDATA");
strcat(appdata, "\zip to save.zip");
printf("Appdata: %sn", appdata); 
strcpy(outfilename, appdata);