我已经阅读了一堆关于将 std::cout 重定向到 stringstreams 的帖子,但是我在读取重定向的字符串时遇到了问题。
std::stringstream redirectStream;
std::cout.rdbuf( redirectStream.rdbuf() );
std::cout << "Hello1\n";
std::cout << "Hello2\n";
while(std::getline(redirectStream, str))
{
// This does not work - as the contents of redirectStream
// do not include the '\n' - I only see "Hello1Hello2"
}
我需要在初始输出中挑选出新行 - 谁能告诉我如何做到这一点?
谢谢。