Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个用 C++ 编写的控制台应用程序。有没有办法将stdout它的所有输出收集到字符串/管道/内存数组?
stdout
PS。我需要从我需要从中收集的控制台应用程序中执行此操作stdout。或者,换句话说,它是从自身收集的。
是的。要将其重定向到 a string,您可以使用 astd::stringstream
string
std::stringstream
std::stringstream buffer; std::streambuf * old = std::cout.rdbuf(buffer.rdbuf());
然后,如果你这样做:
std::cout << "Example output" << std::endl; std::string text = buffer.str();
您将看到text现在包含"Example output\n".
text
"Example output\n"