Подтвердить что ты не робот

Lib curl в С++ отключить печать

У меня есть небольшая программа из http://curl.haxx.se/, и во время ее запуска всегда печатается веб-страница, как я могу отключить функцию печати

#include <iostream>
#include <curl/curl.h>
using namespace std;

int main() {
    CURL *curl;
      CURLcode res;

      curl = curl_easy_init();
      if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1);
        res = curl_easy_perform(curl);

        /* always cleanup */
        curl_easy_cleanup(curl);
      }
      return 0;
}
4b9b3361

Ответ 1

Вам нужно настроить CURLOPT_WRITEFUNCTION, чтобы он не использовал stdout.

Здесь есть объяснение (под CURLOPT_WRITEFUNCTION): http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

и здесь (в разделе "Обращение с Easy libcurl): http://curl.haxx.se/libcurl/c/libcurl-tutorial.html

В основном добавление функции:

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
   return size * nmemb;
}

и вызов

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

Должно это сделать.

Ответ 2

Вы все равно можете получить диагностические сообщения. Чтобы остановить их, вы можете изменить или добавить следующую строку:

curl_easy_setopt (curl, CURLOPT_VERBOSE, 0L); //0 disable messages

Ответ 3

Что сработало для меня, было использование опции CURLOPT_NOBODY в коде, на которую здесь ссылаются: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTNOBODY

#include <iostream>
#include <curl/curl.h>
using namespace std;

int main() {
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1);

        //USING CURLOPT NOBODY
        curl_easy_setopt(curl, CURLOPT_NOBODY,1);

        res = curl_easy_perform(curl);

        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    return 0;
}