错误:无法转换“std::string”到“char*”在初始化中

发布于 2024-12-08 08:17:14 字数 1309 浏览 0 评论 0原文

我有Python脚本,可以从网页下载汇率,我想从中制作C++程序,这是我到目前为止所拥有的:

include iostream
include time.h
include stdio.h
include curl/curl.h
include curl/easy.h
include string
define CURL_STATICLIB

using namespace std;

void dat(string &d){
    time_t rawtime;
    struct tm * timeinfo;
    char datum[80];
    time ( &rawtime );
    timeinfo=localtime(&rawtime);
    strftime(datum,80,"%d%m%y",timeinfo);
    d=datum;
}

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()
{
    string f;
    dat(f);
    string l1="http://www.hnb.hr/tecajn/f";
    string l2=".dat";
    string linkz=l1+f+l2;
    cout << linkz;

    CURL *curl;
    FILE *fp;
    CURLcode res;
    char *url = linkz;
    char outfilename[FILENAME_MAX] = "/home/tomi/data.txt";
    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);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);


    return 0;
}

当我尝试编译时,它给了我这个错误,我发现使用了下载txt的算法,所以我希望它是正确的

i have python script that download exchange rates from web page, and i want make c++ program from that, here is what i have so far:

include iostream
include time.h
include stdio.h
include curl/curl.h
include curl/easy.h
include string
define CURL_STATICLIB

using namespace std;

void dat(string &d){
    time_t rawtime;
    struct tm * timeinfo;
    char datum[80];
    time ( &rawtime );
    timeinfo=localtime(&rawtime);
    strftime(datum,80,"%d%m%y",timeinfo);
    d=datum;
}

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()
{
    string f;
    dat(f);
    string l1="http://www.hnb.hr/tecajn/f";
    string l2=".dat";
    string linkz=l1+f+l2;
    cout << linkz;

    CURL *curl;
    FILE *fp;
    CURLcode res;
    char *url = linkz;
    char outfilename[FILENAME_MAX] = "/home/tomi/data.txt";
    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);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        fclose(fp);


    return 0;
}

it give me this error when i try to compile, i found used algorythm for download txt, so i hope it is correct

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方的巷 2024-12-15 08:17:14

如果您指出了出现错误的行,我就不必跟踪它:

string linkz=l1+f+l2;
...
char *url = linkz;

您可以使用 c_str() 来获取指向 const 字符的指针细绳。这样就可以了:

char const* url = linkz.c_str();

您可以在 setopt 调用中使用同一行,或者将 url 也设置为 std::string

If you'd have pointed out the line where you got the error, I wouldn't have had to track it down to:

string linkz=l1+f+l2;
...
char *url = linkz;

You can use c_str() to get a pointer to the const characters in the string. So this will do:

char const* url = linkz.c_str();

You could have that very same line in the setopt call, or have url be an std::string as well.

手长情犹 2024-12-15 08:17:14

char *url = linkz; 应该是 const char* url = linkz.c_str(); 假设您出于 API 原因确实需要 C 样式字符串。

char *url = linkz; should be const char* url = linkz.c_str(); assuming you really need a C-style string for API reasons.

最近可好 2024-12-15 08:17:14

问题出在这一行:

char *url = linkz;

“links”是一个 std::string,但“url”是一个 char *。尝试使用字符串的 c_str 方法来获取您需要的内容,如下所示:

const char * url = links.c_str();

The problem is in this line:

char *url = linkz;

"links" is an std::string, but"url" is a char *. Try using the c_str method of string to get what you need like so:

const char * url = links.c_str();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文