Redirecting stdout to a file in C through code

前端 未结 3 1143
梦谈多话
梦谈多话 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:55

    You probably want to use freopen.

    Example from reference:

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

提交回复
热议问题