C ++ redirect result

Is there a way to redirect C ++ output inside the code? The situation is that I use some external .cpp and .h files that use printf to put warnings in the console. I want to redirect only these outputs (not mine) to a file without changing my code.

So, in my program, I can redirect the output to a file, and when I add the output redirection to the console again by default, then the file again, and so on ...

Is it possible?

+5
source share
2 answers

You can use freopen () for stdout to redirect stdout to a file.

+7
source

printf 1, , fd, , 1, fd , .

close(1);, int fd = open(file); fd 1, - . , fd 1, .

, lowest available file descriptor number, 1, , printf.

@roe, dup() 1 , fd, stdout.

+1

All Articles