首页 > 解决方案 > 将 CURLcode res 转换为 char*

问题描述

我想接收要使用的网页内容。所以我需要将内容存储在变量char*/string

由于您可以显示内容,printf因此我试图使用它sprintf来查看我是否可以存储内容,但它根本不起作用

int main () {
    CURL *curl = curl_easy_init();
    char rstr[80];

    if(curl) {
        CURLcode res;
        curl_easy_setopt(curl, CURLOPT_URL, "https://api.binance.com/api/v3/png");
        res  = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
        printf("%u\n",res);
        sprintf(rstr,"%u\n",res);
    }

    printf("%s\n",rstr);
    return 0;
}

输出是:

<html><body><h2>404 Not found</h2></body></html>0
0

虽然期待:

<html><body><h2>404 Not found</h2></body></html>0
<html><body><h2>404 Not found</h2></body></html>0

这意味着printf("%u\n",res);显示内容和额外的 0。
sprintf(rstr,"%u\n",res);仅包含 0 而没有站点内容。

如何将内容转换/存储为char[],char*string?

标签: ccurllibcurl

解决方案


推荐阅读