无法存储libcurl结果。(输出空文件或不写入内存)
我正在尝试建立一个connect4,它可以在游戏中击败你,因为我不是人工智能专家,所以我决定从一个网站上获取计算机的操作,我得到一个JSON文件。 我尝试过许多解决方案,但libcurl的效果最好。我设法在提示符中得到了正确的输出 这很奇怪,因为当我不想把它放在内存中时,我得到了一个显示,当我试图保存它时,它就消失了。我尝试了不同的网站,但我想要的网站受到了影响 这是我们想要的 但当我试图将字符串放入内存或文件时,它什么也不写。我使用了libcurl提供的代码 我只是得到一个空文件 我说写了0个字节无法存储libcurl结果。(输出空文件或不写入内存),c,libcurl,C,Libcurl,我正在尝试建立一个connect4,它可以在游戏中击败你,因为我不是人工智能专家,所以我决定从一个网站上获取计算机的操作,我得到一个JSON文件。 我尝试过许多解决方案,但libcurl的效果最好。我设法在提示符中得到了正确的输出 这很奇怪,因为当我不想把它放在内存中时,我得到了一个显示,当我试图保存它时,它就消失了。我尝试了不同的网站,但我想要的网站受到了影响 这是我们想要的 但当我试图将字符串放入内存或文件时,它什么也不写。我使用了libcurl提供的代码 我只是得到一个空文件 我说写了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(应该传递给该函数的流)。再看看你的:
结果是您的请求实际上已发送,回复已收到,但数据并未写入任何位置
更新:
你更新的帖子中的代码对我来说很好。因此,我认为你的问题在别处。请尝试以下操作,而不是原始的文件处理部分:
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" );
}
请注意,虽然教程通常会跳过正确的错误处理,但我上面展示的应该是真实代码的一部分。另外,重复。很抱歉,我的帖子不清楚,我知道我发布的代码不应该在内存中写入任何内容。当我使用教程中的代码时,它不会写任何东西,也不会有任何错误。@ArthurMilon:那么请发布对您不起作用的确切代码,因为教程中的确切代码工作得很好,我们无法找到您没有显示给我们的错误。.-这就是a的用途,也是SO或多或少需要它的原因。我已经更新了帖子,非常感谢它的有效性!!!!在对我的旧代码进行了一些测试之后,它只是在connect4.gamesolver.org google上出现了故障,而其他一些代码工作得很好。但你似乎已经解决了我的问题。我将尝试类似的解决方案来存储在内存中。再次感谢你
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" );
}