[英]Redirecting stdout to a file in C through code
我输出到stdout。 如何通过代码将其重定向到新文件? 在我们运行程序时,我们可以像./sample > test.txt
一样重定向。 如何在执行示例程序时执行此操作? (C编程)
你可能想用freopen 。
参考示例:
#include <stdio.h>
...
FILE *fp;
...
fp = freopen ("/tmp/logfile", "a+", stdout);
使用freopen()
。
使用dup2()
系统调用并将输出重定向到文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.