gpt4 book ai didi

c++ - 为什么这不起作用? libcurl 和 C++

转载 作者:行者123 更新时间:2023-11-30 04:23:11 25 4
gpt4 key购买 nike

我是 libcurl 的新手,正在尝试使用 ftp 从服务器获取文件列表,通过这里的示例和其他一些帖子,我想出了以下代码。但是当我运行它时,它返回错误消息:

Failed writing body (4294967295 != 129)

在 CURLOPT_ERRORBUFFER 设置的错误字符串中。 curl_easy_strerror( res ) 返回:

Failed writing received data to disk/application

struct FtpFile 
{
const char *filename;
FILE *stream;
};

static size_t fileWrite( void *buffer, size_t size, size_t nmemb, void *stream )
{
struct FtpFile *out=(struct FtpFile *)stream;
if(out && !out->stream)
{
out->stream=fopen(out->filename, "wb");
if(!out->stream)
{
cout << out->filename << " open failure [fileWrite] " << endl;
return -1;
}
}

size_t written = fwrite(buffer, size, nmemb, out->stream);
cout << written << endl;
if(written <= 0)
cout << "Nothing written : " << written;

return written;
}

void getFileList( const char* url, const char* fname )
{
CURL *curl;
CURLcode res;
FILE *ftpfile;
const char *errmsg;


ftpfile = fopen(fname, "wb");
if ( ftpfile == NULL )
{
cout << fname << " open failure [getFileList] " << endl ;
return;
}

curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fileWrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, ftpfile);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
curl_easy_setopt(curl, CURLOPT_USERNAME, "username");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "password");
curl_easy_setopt(curl, CURLOPT_FTPLISTONLY,TRUE);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
{
fprintf( stderr, "curl_easy_perform() failed: %s\nError Message: %s\n", curl_easy_strerror( res ), errmsg );
}

curl_easy_cleanup(curl);
}

fclose(ftpfile);
}

int main(int argc, char *argv[])
{
getFileList( "ftp://ftp.example.com/public/somefolder/", "file-list.txt" );

system("PAUSE");
return EXIT_SUCCESS;
}

最佳答案

4294967295 是 -1 的 32 位无符号版本,这可能是您的回调返回的内容,因此 libcurl 认为这是一个错误并停止了一切。

->文件名分配在哪里?

您将 FILE * 传递给 CURLOPT_WRITEDATA,但您的回调将其读取为 struct FtpFile *...

关于c++ - 为什么这不起作用? libcurl 和 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13498313/

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