Rerouting stdin and stdout from C

前端 未结 8 1491
终归单人心
终归单人心 2020-11-22 12:15

I want to reopen the stdin and stdout (and perhaps stderr while I\'m at it) filehandles, so that future calls to printf()

8条回答
  •  温柔的废话
    2020-11-22 12:56

    This is a modified version of Tim Post's method; I used /dev/tty instead of /dev/stdout. I don't know why it doesn't work with stdout (which is a link to /proc/self/fd/1):

    freopen("log.txt","w",stdout);
    ...
    ...
    freopen("/dev/tty","w",stdout);
    

    By using /dev/tty the output is redirected to the terminal from where the app was launched.

    Hope this info is useful.

提交回复
热议问题