使用C ++从控制台应用程序收集stdout输出 [英] Collect stdout output from a console app with C++
本文介绍了使用C ++从控制台应用程序收集stdout输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用C ++编写的控制台应用程序。有没有办法收集所有 stdout
输出到字符串/管道/内存数组?
I have a console application written with C++. Is there any way to collect all stdout
output from it to string/pipe/memory array?
PS。我需要在控制台应用程序内,我需要从 stdout
收集。
PS. I need to do this from within the console app that I'm needing to collect stdout
from. Or, in other words, it is collecting from itself.
推荐答案
是的。要将其重定向到字符串
,可以使用 std :: stringstream
Yes. To redirect it to a string
, you can use a 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
code>输出示例\\\
。
You will see that text
now contains "Example output\n"
.
这篇关于使用C ++从控制台应用程序收集stdout输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文