简体   繁体   中英

Save a file with a absolute path on Linux using libcurl in C++

How can i download a file from a webserver, and save it in a specific path on Linux?

I have used this code (this is an expample):

CURL *curl;
FILE *fp;
CURLcode res;
const char *url = "http://google.com";
char outfilename[FILENAME_MAX] = "\\home\\user_name\\";
curl = curl_easy_init();
if (curl)
{
    fp = fopen(outfilename,"wb");
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    fclose(fp);
}

But it doesn't work perfectly, because it save the file with the absolute path in his name only in the working directory!

Can anyone help me with this problem? Thank you for your attention!

我想你需要使用正斜杠

char outfilename[FILENAME_MAX] = "/home/user_name/";

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM