During a call to std::cout<<data.rdbuf() , std::cout cannot read the characters from data filebuf because the read position is already at the end of the file after the previous output; accordingly , this sets failbit to std::cout , and until this state is cleared, any further output will not work either (i.e. your final line is essentially ignored).
std::cout<<data.str()<<std::endl; will not cause cout go into a failed state, because data.str() returns a copy of the base string regardless of where it was read (in any case, for strings in mixed mode).
source share