Redirect FROM stderr to another file descriptor

后端 未结 2 617
难免孤独
难免孤独 2020-12-21 15:32

My program calls library functions which print to stderr. I want to intervene so that all write calls to file descriptor #2 will instead get sent to somewhere else.

相关标签:
2条回答
  • 2020-12-21 16:05

    You reversed the arguments: it's

    dup2(from_fd, to_fd)
    

    i.e.

    dup2(fd, 2)
    

    (see POSIX.2008 or your manpages.)

    0 讨论(0)
  • 2020-12-21 16:15

    Just for completeness: You could even achieve your goal with freopen(name, mode, stderr), which is a Standard/ANSI/ISO C89 and C99 function.

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