Libcurl自动将换行替换为换行+回车



正如标题所说,当下载一些东西并保存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;
}

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

和修复是很容易的:只需使用b(二进制)模式字符串在open:

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

最新更新