Collect stdout output from a console app with C++

杀马特。学长 韩版系。学妹 提交于 2019-12-13 04:28:03

问题


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. 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.


回答1:


Yes. To redirect it to a string, you can use a std::stringstream

std::stringstream buffer;
std::streambuf * old = std::cout.rdbuf(buffer.rdbuf());

Then, if you do:

std::cout << "Example output" << std::endl;
std::string text = buffer.str();

You will see that text now contains "Example output\n".



来源:https://stackoverflow.com/questions/15419523/collect-stdout-output-from-a-console-app-with-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!