【发布时间】:2016-06-01 17:09:25
【问题描述】:
我可以用dup2(或fcntl)做一些魔术吗,以便我将stdout重定向到一个文件(即,写入描述符1的任何内容都会转到一个文件),但是如果我使用了一些其他机制,它会去终端输出?如此松散:
int original_stdout;
// some magic to save the original stdout
int fd;
open(fd, ...);
dup2(fd, 1);
write(1, ...); // goes to the file open on fd
write(original_stdout, ...); // still goes to the terminal
【问题讨论】: