如何在Linux上使用变量?
在libcurl中设置绝对路径这是一个示例代码:
}
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);
}
但编译器会返回此错误:
error: array must be initialized with a brace-enclosed initializer
有谁知道如何修复它?谢谢你的关注!
答案 0 :(得分:1)
使用c_str()
类型的std::string
method
e.g。
const char* outfilename= absolute_path.c_str();
P.S。你有客观的理由声明const char数组吗?