YAKOVM
YAKOVM

Reputation: 10153

Redirect the stdout to buffer,not using >

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!

Upvotes: 1

Views: 1506

Answers (1)

RedX
RedX

Reputation: 15165

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.

Upvotes: 7

Related Questions