I am outputting to stdout. How can I redirect that to a new file through code? While we run the program we can redirect like ./sample > test.txt. How can I d
./sample > test.txt
Use dup2() system call and redirect the output to a file.
dup2()
You probably want to use freopen.
Example from reference:
#include <stdio.h> ... FILE *fp; ... fp = freopen ("/tmp/logfile", "a+", stdout);
Use freopen().