Redirecting stdout to a file in C code

I output to stdout. How can I redirect this to a new file using code? While we run the program, we can redirect as ./sample > test.txt . How to do this when executing the sample program itself? (C programming)

+4
source share
3 answers

You probably want to use freopen .

Example from the link:

 #include <stdio.h> ... FILE *fp; ... fp = freopen ("/tmp/logfile", "a+", stdout); 
+7
source

Use freopen() .

+3
source

Use the dup2() system call and redirect the output to a file.

0
source

Source: https://habr.com/ru/post/1307072/


All Articles