gpt4 book ai didi

无法存储 libcurl 结果。 (输出空文件或不写入内存)

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

我正在尝试构建一个可以在游戏中击败你的 connect4,由于我不是人工智能专家,所以我决定从网站获取计算机的 Action (我得到一个 JSON 文件)。我尝试了很多解决方案,但 libcurl 效果最好。我设法在提示中获得正确的输出。

这很奇怪,因为当我不想将其放入内存时,我会得到一个显示,并且一旦我尝试保存它,它就会消失。我尝试过不同的网站,只有我想要的网站受到影响。

这是想要的 JSON http://connect4.gamesolver.org/solve?pos=44

但是当我尝试将字符串放入内存或文件中时,它什么也没写。 (我使用了libcurl提供的代码...

https://curl.haxx.se/libcurl/c/url2file.html我只得到一个空文件

https://curl.haxx.se/libcurl/c/getinmemory.html我说的是 0 字节写入)。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <curl/curl.h>

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}

int main(int argc, char *argv[])
{
CURL *curl_handle;
static const char *pagefilename = "page.out";
FILE *pagefile;

if(argc < 2) {
printf("Usage: %s <URL>\n", argv[0]);
return 1;
}

curl_global_init(CURL_GLOBAL_ALL);

/* init the curl session */
curl_handle = curl_easy_init();

/* set URL to get here */
curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]);

/* Switch on full protocol/debug output while testing */
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L);

/* disable progress meter, set to 0L to enable and disable debug output */
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);

/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

/* open the file */
pagefile = fopen(pagefilename, "wb");
if(pagefile) {

/* write the page body to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

/* get it! */
curl_easy_perform(curl_handle);

/* close the header file */
fclose(pagefile);
}

/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);

curl_global_cleanup();

return 0;
}

最佳答案

您没有设置 CURLOPT_WRITEFUNCTION(libcurl 应回调以实际写入数据的函数)和 CURLOPT_WRITEDATA(应传递给该函数的流)。再看看你的first tutorial :

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}

int main(int argc, char *argv[])
{
// ...
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

/* open the file */
pagefile = fopen(pagefilename, "wb");
if(pagefile) {

/* write the page body to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

/* get it! */
curl_easy_perform(curl_handle);

/* close the header file */
fclose(pagefile);
}

// ...
}

结果是您的请求实际上已发送,收到了回复,但数据没有写入任何地方。

更新:

您更新的帖子中的代码对我来说效果很好。因此,我认为您的问题出在其他地方。尝试这个而不是原来的文件处理部分:

  char errorbuf[ CURL_ERROR_SIZE ] = "";
curl_easy_setopt(curl_handle, CURLOPT_ERRORBUFFER, errorbuf);

/* open the file */
pagefile = fopen(pagefilename, "wb");
if(pagefile) {

/* write the page body to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);

/* get it! */
if ( curl_easy_perform(curl_handle) != CURLE_OK )
{
fputs( errorbuf, stderr );
}

/* close the header file */
fclose(pagefile);
}
else
{
perror( "fopen failed" );
}

请注意,虽然教程通常会跳过正确的错误处理,但我上面展示的内容应该是“真实”代码的一部分。

关于无法存储 libcurl 结果。 (输出空文件或不写入内存),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57989119/

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