This is a well-known method for copying a stream to another using rdbuf:
#include <iostream>
#include <fstream>
int main()
{
std::ifstream in{"/tmp/foo.txt"};
std::cerr << in.rdbuf();
std::cerr << "Done\n";
}
However, this aborts (= sets the bad bit) my cerrwhen /tmp/foo.txtempty. The result is Done\nnot displayed.
Why? Observed with g ++ / libstdC ++ / GNU Linux and Clang ++ / libC ++ / OS X.
source
share