通过代码将stdout重定向到C中的文件

Ato*_*tom 4 c file

我输出到stdout.如何通过代码将其重定向到新文件?当我们运行程序时,我们可以重定向./sample > test.txt.如何在执行示例程序时执行此操作?(C编程)

tva*_*son 7

你可能想用freopen.

参考示例:

#include <stdio.h>
...
FILE *fp;
...
fp = freopen ("/tmp/logfile", "a+", stdout);
Run Code Online (Sandbox Code Playgroud)