libcurl 自动用换行+回车替换换行return
libcurl automatically replacing line feed with line feed + carriage return
如标题所述,在下载内容并保存 libcurl 时将所有 LF 替换为 LF + CR。这对文本文件很好。但对于二进制来说,这是一场灾难。我已经试过了
curl_easy_setopt(curl, CURLOPT_CRLF, 0L);
如何禁用这个东西。我 运行 windows 并且卷曲 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 (\r\n
),而在 C 或 C++ 中仅表示为 \n
。
而且修复非常简单:只需在 open
中的模式字符串中使用 b
(对于二进制):
pFile = fopen ("myfile.png","wb");
如标题所述,在下载内容并保存 libcurl 时将所有 LF 替换为 LF + CR。这对文本文件很好。但对于二进制来说,这是一场灾难。我已经试过了
curl_easy_setopt(curl, CURLOPT_CRLF, 0L);
如何禁用这个东西。我 运行 windows 并且卷曲 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 (\r\n
),而在 C 或 C++ 中仅表示为 \n
。
而且修复非常简单:只需在 open
中的模式字符串中使用 b
(对于二进制):
pFile = fopen ("myfile.png","wb");