Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lib curl in c++ disable printing

Tags:

c++

i got a small program from http://curl.haxx.se/ and while i run it always prints the webpage how can i disable the printing function

#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;
}
like image 980
raj Avatar asked Aug 26 '10 05:08

raj


3 Answers

You need to set up a CURLOPT_WRITEFUNCTION to make it not use stdout.

There is an explanation here (under CURLOPT_WRITEFUNCTION): http://curl.haxx.se/libcurl/c/curl_easy_setopt.html

and here (Under "Handling the Easy libcurl): http://curl.haxx.se/libcurl/c/libcurl-tutorial.html

Basically adding the function:

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

and calling

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

Should do it.

like image 67
Ashaman Avatar answered Oct 25 '22 07:10

Ashaman


You can still get diagnostic messages. To stop these either change or add the following line:

curl_easy_setopt (curl, CURLOPT_VERBOSE, 0L); //0 disable messages
like image 22
Anton Andreev Avatar answered Oct 25 '22 07:10

Anton Andreev


To write data into file instead of printing, give a file descriptor as:

FILE *wfd = fopen("foo.txt", "w");
...
curl_easy_setopt(curl, CURLOPT_WRITEDATA, wfd);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
like image 40
Nitinkumar Ambekar Avatar answered Oct 25 '22 06:10

Nitinkumar Ambekar