Redirecting stdout to a file in C through code

前端 未结 3 1117
梦谈多话
梦谈多话 2021-01-13 20:19

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 d

相关标签:
3条回答
  • 2021-01-13 20:49

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

    0 讨论(0)
  • 2021-01-13 20:55

    You probably want to use freopen.

    Example from reference:

    #include <stdio.h>
    ...
    FILE *fp;
    ...
    fp = freopen ("/tmp/logfile", "a+", stdout);
    
    0 讨论(0)
  • 2021-01-13 21:10

    Use freopen().

    0 讨论(0)
提交回复
热议问题