You can copy the content to a std::ostringstream
and retrieve a copy of the buffer using str()
:
std::ostringstream buf;
std::copy(directory_iterator(p), directory_iterator(),
std::ostream_iterator<directory_entry>(buf, "\n"));
std::string content(buf.str());