开发者

Redirecting stdout to a file in C through code

开发者 https://www.devze.com 2022-12-26 19:36 出处:网络
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 do this when executing t开发者_如何学Pyth

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 do this when executing t开发者_如何学Pythonhe sample program itself ? (C programming)


You probably want to use freopen.

Example from reference:

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


Use freopen().


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

0

精彩评论

暂无评论...
验证码 换一张
取 消