1

I wrote my implementation of printf – myPrintf,which prints to stdout. I want to verify that it works fine.In order to check the correctess of the printed output I want to compare it with char I expect to get. How can I write code to redirect the stdout to buffer,not using >.

I can use only printf!

4
  • Why are you opposed to teeing the output to a file with >? Commented Aug 16, 2011 at 14:45
  • If this is C++ why not use iostreams instead? Then "capturing" the output is simply a question of using a stringstream Commented Aug 16, 2011 at 14:49
  • Without knowing what you are actually using to print it out, its hard to tell what to use to capture the output. Commented Aug 16, 2011 at 14:55
  • I have to redirect from stdout to char buffer Commented Aug 16, 2011 at 17:55

1 Answer 1

7

You could redirect couts buffer by setting it's rdbuf() to a file you have opened.

Weird, C++ and only printf, but whatever.

It's also possible to redirect stdout in C.

Here is one way of doing it: https://rydow.wordpress.com/2007/10/26/c-code-to-redirect-stdout/

It involves dup and dup2.

There is also this option ( Rerouting stdin and stdout from C ) using freopen.

Sign up to request clarification or add additional context in comments.

1 Comment

Or to a custom std::streambuf that copies into some place in memory (think std::ostringstream).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.