dup2 redirect stdout stderr
2016-04-06 22:33
471 查看
#include<stdio.h> #include<stdlib.h> #include<fcntl.h> void main() { int fd; fd = open("/tmp/leo.log", O_WRONLY | O_CREAT | O_APPEND, 0640); if(fd < 0) { fd = open("/dev/null", O_WRONLY); } // dup will not success // dup(fd, 1); // dup(fd, 2); dup2(fd, 1); dup2(fd, 2); close(fd); printf("test string\n"); fprintf(stderr, "test string 2\n"); }
$ ./a.out
$ cat leo.log
test string 2
test string
------------- 使用dup的情况:-----------
$ ./a.out
test string
test string 2
Advanced Programming in the UNIX® Environment: UNIX File I/O
http://www.informit.com/articles/article.aspx?p=99706&seqNum=12
Linux管道编程技术:dup函数,dup2函数,open函数详解
http://blog.csdn.net/zhouhong1026/article/details/8151235
相关文章推荐
- D20
- 16Red and Black
- Linux tftp配置
- hdu5558 后缀数组
- SVN命令使用详解
- leetcode——2——Add Two Numbers
- kernel_init寻找init并执行
- 实例详解机器学习如何解决问题
- androidStudio出现R无法识别比对下面build.gradule
- Android学习之Soft Keyboard使用文档翻译
- Mac电脑编程问题汇总.
- 字符串排序
- 复利计算5.0
- for 循环学习,输出
- LeetCode(28)-Remove Duplicates from Sorted Array
- xml入门及bom
- spring+tomcat8实现websocket
- Tsinsen A1129 税收与补贴问题
- LeetCode(28)-Remove Duplicates from Sorted Array
- LeetCode(28)-Remove Duplicates from Sorted Array