If such a function in c ++?

Just want to rewrite

function Parser_url($url){ $test = array('http'=>array('method'=>"GET",'header'=>"User-Agent: TEST\r\n")); $create = stream_context_create($test); $parser = file_get_contents($url, false, $create);} 

php to c ++

    2 answers 2

    Ran into a similar question on SO , here is a brief overview of the options:

    If std::filebuf , then this is the fastest option suggested by Kitty :

     std::ifstream file(filename); std::streambuf* content = file.rdbuf(); 

    If you want std::string , then something like this:

     std::ifstream file(filename); std::string content( (std::istreambuf_iterator<char>(file) ), (std::istreambuf_iterator<char>() ) ); 

    Similarly, you can shove in std::vector<char> .

    Well, if you need char* , then:

     FILE* f = fopen(filename, "r"); // Получаем размер файла fseek(f, 0, SEEK_END); size_t size = ftell(f); // Выделяем память, читаем данные char* content = new char[size]; // Или (char *)malloc(size), тогда будет чистый C. rewind(f); fread(where, sizeof(char), size, f); 

    BUT: this is all for files in the local file system. For HTTP, you can do Boost.Asio with something in the spirit of

     ip::tcp::iostream stream; stream.expires_from_now(boost::posix_time::seconds(60)); stream.connect("www.boost.org", "http"); stream << "GET /LICENSE_1_0.txt HTTP/1.0\r\n"; stream << "Host: www.boost.org\r\n"; stream << "Accept: */*\r\n"; stream << "Connection: close\r\n\r\n"; stream.flush(); std::streambuf* content = stream.rdbuf(); 

    Or use, for example, Urdl :

     urdl::istream stream("http://www.boost.org/LICENSE_1_0.txt"); std::streambuf* content = stream.rdbuf(); 

    Or use libcurl (there are examples by reference).

    • ATP all works =) - Riolu

    There is no such function in standard libraries and STL. Here is the code, but did not check it.

    1. Do you already have the file you want to receive on the drive?
    2. What data type do you want to get the file contents (std::string , char *) ?

    Checked it works. Examples below.

     #include <fstream> static int file_get_contents(std::string file_name, char *&content_file, long long &size_file) { if (file_name.empty()) { return -1; } std::fstream reader(file_name, std::ios::in | std::ios::binary); if (!reader) { std::cout << "n Файл не существует или его невозможно открыть на чтение! - " << file_name; return -2; } reader.seekg(0, std::ios::end); size_file = reader.tellg(); reader.seekg(0, std::ios::beg); content_file = new char[size_file]; reader.read(content_file, size_file); reader.close(); if (size_file == 0) { return -3; } content_file[size_file] = 0; return 1; } //// Примеры. // // применение char * content = 0; long long size = 0; file_get_contents("файл.txt", content, size); std::cout << "\nсодержимое 1=" << content; // Для std::string std::string text; char * content2; size = 0; file_get_contents("файл.txt", content2, size); text.append(content2, size); std::cout << "\nсодержимое 2=" << text;