1

How can i set the absolute path in libcurl on Linux, using a variable?

Here's an example code:

   }
    string absolute_path = "/home/user_name";
    CURL *curl;
    FILE *fp;
    CURLcode res;
    const char *url = "http://google.com";
    const char outfilename[FILENAME_MAX] = absolute_path;
    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 the compiler returns this error:

error: array must be initialized with a brace-enclosed initializer

Does anyone know how to fix it? Thank you for your attention!

1 Answers1

1

Use the c_str() method of std::string type

e.g.

const char* outfilename= absolute_path.c_str();

P.S. Do you have an objective reason to declare the const char array ?

deimus
  • 9,565
  • 12
  • 63
  • 107
  • Thanks for the reply, but i already try it but the compiler returns the same error. Now, i'm using directly the method fopen() as user3159253 told me, and it works. P.S: I made a mistake, i have declared the variable as const char array because I was experiencing (for example. i had to declare const char *url as a const char to using a variable! – QuestionMan Sep 19 '14 at 14:02
  • A simple example like this `std::string path = "myValue"; const char* src = path.c_str();` should never fail. Obviously you had another issue – deimus Sep 19 '14 at 14:11