gpt4 book ai didi

c++ - cURL 条件终止下载

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:12:24 29 4
gpt4 key购买 nike

我正在使用 libcurl 下载二进制文件,但我想读取这些二进制文件的 header ,即前几个字节。如果 header 字节匹配我要继续下载的条件,否则我不想取消下载。

size_t writeData(void *contents, size_t size, 
size_t nmemb, FILE *stream) {

const unsigned char * cPtr;
cPtr = (const unsigned char*)contents;
bool isByte = checkByte(cPtr, nmemb);
if (isByte){
// Continue Download, and write to disk.
size_t written = fwrite(contents, size, nmemb, stream);
return written;
}
else
// Kill Download.
}

最佳答案

定义您自己的写函数设置选项 CURLOPT_WRITEFUNCTION。在此处查看文档:https://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html .

要中止传输,您只需从写入函数返回值,该值小于传递给写入函数的字节数。文档中的相应行:

If that amount differs from the amount passed to your callback function, it'll signal an error condition to the library. This will cause the transfer to get aborted and the libcurl function used will return CURLE_WRITE_ERROR

关于c++ - cURL 条件终止下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39023152/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com