【发布时间】:2009-12-10 15:07:19
【问题描述】:
我已经阅读了很多关于将 std::cout 重定向到字符串流的帖子,但是我在读取重定向的字符串时遇到了问题。
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"
}
我需要在初始输出中挑选出新行 - 谁能告诉我如何做到这一点?
谢谢。
【问题讨论】:
-
供您参考,这适用于我的机器。 (Visual C++ 2008)
标签: c++ redirect stringstream cout