5

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)

mdml
  • 22,442
  • 8
  • 58
  • 66
Atom
  • 347
  • 2
  • 4
  • 12

3 Answers3

8

You probably want to use freopen.

Example from reference:

#include <stdio.h>
...
FILE *fp;
...
fp = freopen ("/tmp/logfile", "a+", stdout);
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
3

Use freopen().

Bastien Léonard
  • 60,478
  • 20
  • 78
  • 95
0

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

mdml
  • 22,442
  • 8
  • 58
  • 66