【问题标题】:Libcurl how to not show all this informationLibcurl 如何不显示所有这些信息
【发布时间】:2013-12-07 22:47:45
【问题描述】:

如何不显示所有这些信息? 我所做的只是使用经过少量编辑的 FTP 示例,我不希望显示该信息。

编辑:添加来自 main.c 的完整代码

链接中的图片:http://www.mediafire.com/convkey/71e9/oyhctzcdjxakzxzfg.jpg

#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>

struct FtpFile {
 const char *filename;
 FILE *stream;
};
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
  struct FtpFile *out=(struct FtpFile *)stream;
  if(out && !out->stream) {
  /* open file for writing */
  out->stream=fopen(out->filename, "wb");
  if(!out->stream)
  return -1; /* failure, can't open file to write */
}
return fwrite(buffer, size, nmemb, out->stream);
}

int main()
{
CURL *curl;
CURLcode res;
struct FtpFile version={"version.txt", /* name to store the file as if succesful */NULL};

curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();

FILE *file_verzija;
int trenutna_verzija;
int nova_verzija;
char pitanje_za_update;

file_verzija=fopen("version.txt","r");

fscanf(file_verzija,"%i",&trenutna_verzija);

fclose(file_verzija);


printf("Current version %i",trenutna_verzija);

printf("\nChecking for updates...\n");


if(curl)
{
    /*You better replace the URL with one that works!*/curl_easy_setopt(curl, CURLOPT_URL,"http://elektro-srb.com/site/muffin_finder_files/version.txt");
    /* Define our callback to get called when there's data to be written */
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
    /* Set a pointer to our struct to pass to the callback */
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &version);
    /* Switch on full protocol/debug output */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    res = curl_easy_perform(curl);
    /* always cleanup */
    curl_easy_cleanup(curl);
    if(CURLE_OK != res)
    {
        /* we failed */
        printf("\nerror");
    }
}

if(version.stream)
    fclose(version.stream); /* close the local file */

file_verzija=fopen("version.txt","r");

fscanf(file_verzija,"%i",&nova_verzija);

fclose(file_verzija);

if(trenutna_verzija != nova_verzija)
{
    printf("\nUpdate found! New version is %i",nova_verzija);
}
else
{
    printf("You are running latest version of Muffin Finder!");
}

if(trenutna_verzija != nova_verzija)
{
    printf("\nUpdate? y/n");
    scanf("\n%c",&pitanje_za_update);
    if((pitanje_za_update == 'y') || (pitanje_za_update == 'Y'))
    {
        //UPDATE
    }
    else if((pitanje_za_update == 'n') || (pitanje_za_update == 'N'))
    {
        //pokretanje stare
    }
}


curl_global_cleanup();



return 0;
}

【问题讨论】:

    标签: c curl block codeblocks libcurl


    【解决方案1】:

    您应该构造一个WRITEFUNCTION 选项,以防止它使用stdout 进行打印。

    请看这里:http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

    搜索“WRITEFUNCTION”。你应该实现这个函数(我假设你想把它留空)。

    编辑:如手册所述,您应该执行以下操作:

    实现一个函数来替换默认的stdout

    size_t outputFunction(char *ptr, size_t size, size_t nmemb, void *userdata) {}

    当你初始化 CURL 结构时,使用这个选项:

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, outputFunction);

    【讨论】:

    • 实际上我并不是真正的编程神,所以我需要更多帮助:) 你能告诉我具体该怎么做吗?我添加了代码
    • 复制一个是什么意思?我使用了curl.haxx.se 中的 ftpget.c 示例并添加了一些文本文件读取?我只是不明白你让我做什么,所以我请求更详细的帮助。
    • 我无法让它工作:/感谢您的帮助,但我会尝试其他方法,例如隐藏程序窗口并在快板窗口中写入所需的所有内容。再次感谢:)
    • 什么不起作用?这很简单。在代码开头添加函数定义。您应该在 if (curl) 代码块内调用 curl_easy_setopt。如果可以,请将此问题标记为已回答。
    • 我尝试了你所说的一切,但它仍然会在屏幕上打印所有内容。所以没有问题没有得到回答。
    【解决方案2】:

    评论以下行:

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    【讨论】:

    • 谢谢!这正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 2020-04-10
    • 2019-03-02
    相关资源
    最近更新 更多