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!

YAKOVM
  • 9,805
  • 31
  • 116
  • 217

1 Answers1

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.

Community
  • 1
  • 1
RedX
  • 14,749
  • 1
  • 53
  • 76