0

I have a function provided by a library (so I can't edit the source code for it), that prints output to stdout. I wish to get that output in a string variable, so that I can check some conditions on it. What is the best way to do this? Can I do it without multithreading and file I/O?

[EDIT] Similar questions(pointed out in comments):

Using multithreading:

Capture Output of Spawned Process to string

If you know output size beforehand:

redirect output from stdout to a string?

9

1 Answer 1

1

Use rdbuf.

std::stringstream stream;
auto * old = std::cout.rdbuf(stream.rdbuf());
CallFunction();
std::cout.rdbuf(old);
std::string value = stream.str();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.