【问题标题】:libcurl automatically replacing line feed with line feed + carriage returnlibcurl 自动用换行+回车替换换行
【发布时间】:2015-07-22 15:23:58
【问题描述】:

正如标题所说,下载内容并保存 libcurl 时,将所有 LF 替换为 LF + CR。对于文本文档来说很好。但对于二进制来说,这是一场灾难。我已经试过了

curl_easy_setopt(curl, CURLOPT_CRLF, 0L);

如何禁用这个东西。我在 windows 和 curl 7.40.0 上运行

#include <iostream>
#include <curl/curl.h>

using namespace std;

CURL *curl;
CURLcode res;

size_t file_write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
{
    fwrite(ptr,size,nmemb,(FILE *)userdata);
    return nmemb;
}

int main(void)
{
    FILE * pFile;
    pFile = fopen ("myfile.png","w");

    curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, "http://www.dilushan.tk/Media/128px_feed.png");
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    if (pFile!=NULL)
    {
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, pFile);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, file_write_callback);
        res = curl_easy_perform(curl);
    }
    fclose (pFile);
    return 0;
}

【问题讨论】:

    标签: c++ codeblocks libcurl


    【解决方案1】:

    libburl 不是罪魁祸首,而是底层系统库。因为 Windows 有一个不应该发生转换的二进制文件的概念,而文本文件的行尾在磁盘上表示为 CrLf (\r\n),而在 C 或 C++ 中只有 \n。

    而且修复非常简单:只需在open 的模式字符串中使用b(二进制)即可:

    pFile = fopen ("myfile.png","wb");
    

    【讨论】:

    • 感谢您指出我的愚蠢错误。这解决了我的问题
    猜你喜欢
    • 1970-01-01
    • 2020-02-05
    • 2014-12-11
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-28
    相关资源
    最近更新 更多