|
发表于 2005-2-20 02:14:19
|
显示全部楼层
Post by bexley
- dup2(2 , fd);
- close(2);
- dup2(fd , 2);
复制代码
是这个思路吗?我这里不行啊!
没有问题呀,这是我写的代码:
- #include<unistd.h>
- #include<stdlib.h>
- #include<fcntl.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<stdio.h>
- int main()
- {
- int sfd = 0;
- /* 保存标准错误输出 */
- if (-1 == dup2(STDERR_FILENO,sfd) ) {
- perror("can't save fd ");
- exit(1);
- }
- if (-1 == close(STDERR_FILENO) ) {
- perror("can't close stderr");
- exit(1);
- }
- /* 恢复stderr */
- if (-1 != dup2(sfd,STDERR_FILENO) ) {
- printf("recover fd ok \n");
- /* 恢复后,写入stderr应该向屏幕输出 */
- write(STDERR_FILENO,"stderr ok!\n",11);
- }
- }
复制代码 |
|