将 std::ofstream 的内容复制到 std::string

Copy the contents of std::ofstream into an std::string

我有一个尚未刷新的 std::ofstream 对象。有没有办法获取此 ofstream 的内容并将其存储到 std::string?

我想要的本质是:

std::ofstream ofs("somefile.txt");
ofs << "stuff";
someMethod(ofs); //This method streams into ofs as well
ofs <<"blah";
yetAnotherMethod(ofs); //ofs gets violated inside this method as well 
//more data streamed into ofs
std::string s; //Whatever was streamed into ofs should go into s as well for an additional output.

没有。 “std::ofstream 的内容”是荒谬的。文件有内容,流是代表写入文件的句柄。

刷新 ofs 并读取文件,或者使用 std::ostream & 代替 std::ofstream &