Search code examples
c++fileboostboost-filesystem

boost::filesystem How having a correct path to file read it into stringstream?


So I have a path p and I can call for example is_regular_file(p) and file_size(p) on it but how to read that file into stringstream? (btw I need only to read it)


Solution

  • I assume you want to copy the entire file into the stringstream.

    std::stringstream ss;
    ss << std::ifstream( p.string().c_str() ).rdbuf();
    

    Here is a kind of inside-out demo:

    Shadow:code dkrauss$ ./ssclone ssclone.cpp 
    #include <sstream>
    #include <fstream>
    #include <iostream>
    
    int main(int, char *argv[] ) {
    std::stringstream ss;
    ss << std::ifstream( argv[1] ).rdbuf();
    
    std::cout << ss.str() << '\n';
    }