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 strin开发者_JS百科gstream? (btw I need only to read it)
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';
}
boost::filesystem::path has string method.
const std::string & string() const;
http://www.boost.org/doc/libs/1_43_0/libs/filesystem/doc/reference.html#Class-template-basic_path
ss << p.string ().c_str ();
精彩评论