Copy a streambuf’s contents to a string

I don’t know whether it counts as “excessive copying“, but you can use a stringstream: std::ostringstream ss; ss << someStreamBuf; std::string s = ss.str(); Like, to read everything from stdin into a string, do std::ostringstream ss; ss << std::cin.rdbuf(); std::string s = ss.str(); Alternatively, you may also use a istreambuf_iterator. You will have to measure … Read more