I am calling a function funcB from funcA.
funcB uses several printf statements to output data.
Is there a way for me to capture that data via funcA?
I can not modify funcB.
funcB(){
printf( "%s", "My Name is" );
printf( "%s", "I like ice cream" );
}
funcA(){
funcB();
}
(This answer is a corrected version based on this answer.)
This answer is POSIX centric. Use
opento create a file descriptor for the file you want to redirect to. Then, usedup2toSTDOUT_FILENOto changestdoutto write to the file instead. But, you’ll want toduptheSTDOUT_FILENObefore you do that, so you can restorestdoutwith anotherdup2.If
funcBis usingstd::cout, usestd::cout.flush()instead offflush(stdout).If you want to manipulate C++ streams more directly, you can use Johnathan Wakely’s answer.