The URL2File is a small utility which returns a std::ifstream from an URL
Internally it relies simply on wget to download the file locally into a file with a certain temporary name
#include <stdio.h> | |
#include <string> | |
#include <iostream> | |
#include <fstream> | |
#include <memory> | |
#include "URL2File.hpp" | |
using namespace std; | |
int main(int argc, char** argv) | |
{ | |
const string url = "https://httpbin.org/get"; | |
string res; | |
auto temp = URL2File(url)(); | |
while(!temp->eof()) | |
{ | |
string t_res; | |
*temp >> t_res; | |
res += t_res; | |
} | |
std::cout << "Completed " << std::endl; | |
std::cout << "Result = " << res << std::endl; | |
} | |
#include <stdio.h> | |
#include <fstream> | |
#include <stdexcept> | |
#include <memory> | |
class URL2File | |
{ | |
public: | |
URL2File( const std::string& _url ) : url(_url), temp_fn("/tmp/temp.txt"), file(nullptr) {} | |
~URL2File() { pclose(file); } | |
std::shared_ptr<std::ifstream> operator()() | |
{ | |
file = popen(("wget "+url+" -O " + temp_fn).c_str(),"r"); | |
if(!file) throw std::runtime_error("Error while retrieving from url = " + url); | |
return std::shared_ptr<std::ifstream>(new std::ifstream(temp_fn)); | |
//std::ifstream temp(temp_fn); | |
//return temp; | |
} | |
const std::string url; | |
private: | |
std::string temp_fn; | |
FILE* file; | |
}; | |