Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting stdout to a file in C through code

Tags:

c

file

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 the sample program itself ? (C programming)

like image 891
Atom Avatar asked Apr 15 '10 19:04

Atom


1 Answers

You probably want to use freopen.

Example from reference:

#include <stdio.h>
...
FILE *fp;
...
fp = freopen ("/tmp/logfile", "a+", stdout);
like image 196
tvanfosson Avatar answered Oct 13 '22 05:10

tvanfosson