gpt4 book ai didi

c++ - 如何将输出发送到 stdout 到字符串

转载 作者:行者123 更新时间:2023-11-28 05:58:39 25 4
gpt4 key购买 nike

我有一个库提供的函数(因此我无法编辑它的源代码),它会将输出打印到标准输出。我希望在字符串变量中获取该输出,以便我可以检查它的一些条件。做这个的最好方式是什么?如果没有多线程和文件 I/O,我能做到吗?

[编辑] 类似问题(在评论中指出):

使用多线程:

Capture Output of Spawned Process to string

如果您事先知道输出大小:

redirect output from stdout to a string?

最佳答案

使用 rdbuf。

std::stringstream stream;
auto * old = std::cout.rdbuf(stream.rdbuf());
CallFunction();
std::cout.rdbuf(old);
std::string value = stream.str();

关于c++ - 如何将输出发送到 stdout 到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33701162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com