22. Using C++ to Read Documents from Internet

cURL library

The library cURL is needed to read documents from internet. Install the library using one of the following commands (one of them should hopefully work):

sudo apt install curl
sudo apt-get install libcurl4-openssl-dev
sudo apt-get install libcurl4-gnutls-dev

Example of a program that reads data from a website

// visitURL.cpp
// compile with
// c++ visitURL.cpp -o vurl -lcurl -std=c++11
// execute with
// ./vurl

#include <iostream>
#include <string>
#include <fstream>
#include <curl/curl.h>
static size_t f(void *p, size_t s, size_t m, void *u)
 ((std::string*)u)->append((char*)p, s * m);
 return s * m;
std::string readFromURL(const std::string & mainLink){
 CURL *curl;
 std::string result;
 curl = curl_easy_init();
 if(curl) {
   const char* linkArrayChar=mainLink.c_str();
   curl_easy_setopt(curl, CURLOPT_URL, linkArrayChar);
   curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, f);
   curl_easy_setopt(curl, CURLOPT_WRITEDATA, & result);
 return result;
int main(){
 std::string res=readFromURL("https://imomath.com/");
 return 0;