C++ streams confusion: istreambuf_iterator vs istream_iterator?

IOstreams use streambufs to as their source / target of input / output. Effectively, the streambuf-family does all the work regarding IO and the IOstream-family is only used for formatting and to-string / from-string transformation. Now, istream_iterator takes a template argument that says what the unformatted string-sequence from the streambuf should be formatted as, like … Read more

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